Giter Site home page Giter Site logo

bohnacker / p5js-screenposition Goto Github PK

View Code? Open in Web Editor NEW
33.0 3.0 8.0 749 KB

This function provides a solution to the missing screenX and screenY functions in p5.js. See https://github.com/processing/p5.js/issues/1553 for the discussion.

License: MIT License

JavaScript 100.00%
p5js p5 p5-js p5-library screenx screeny

p5js-screenposition's Introduction

p5js-screenPosition

screenPosition-Cover

This function provides a solution to the missing screenX and screenY functions in p5.js. See processing/p5.js#1553 for the discussion.

This is work in progress.

As far as I could find out there should be anything implemented to make it work perfectly in any mode (2D, WEBGL, angleMode DEGREES or RADIANS, normal or instance mode). If you'll find a case that isn't working correctly, please file an issue.

Usage

For a quick look on what it's doing see https://editor.p5js.org/bohnacker/sketches/nUk3bVW7b.

1. Download

Download the latest release of addScreenPositionFunction.js.

2. Use it in your project

Link to it in your HTML file. It's only working in combination with p5.js.

<script src="p5.js"></script>
<script src="addScreenPositionFunction.js"></script>

3. Initializing

Call the function addScreenPositionFunction() after creating the canvas.

function setup() {
  createCanvas(400, 400);
  addScreenPositionFunction();
}

If your using p5.js in instance mode you have to pass the instance:

p.setup = function() {
  p.createCanvas(400, 400);
  addScreenPositionFunction( p );
};

Quite similar if you want to use the function with offscreen graphics:

graphics = createGraphics(400, 400);
addScreenPositionFunction( graphics );

4. Getting the screen position

Now you can use screenPosition(x, y, [z]) to get the position of a coordinate on screen. It returns a p5.Vector.

var p = screenPosition(-100, 50, 0);

Or giving a p5.Vector:

var v = createVector(-100, 50, 0)
var p = screenPosition( v );

Or giving an array with x, y, z coordinates:

var v = [-100, 50, 0];
var p = screenPosition( v );

Acknowledgements

Thanks to Thibault Coppex (@tcoppex) for the 3d-modelview-projection-math he supplied in the issue discussion thread (processing/p5.js#1553). I had to adjust it a bit maybe because p5js changed the way webgl is handled since 2016.

p5js-screenposition's People

Contributors

bohnacker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

p5js-screenposition's Issues

Add Documentation on p5.Graphics?

From what I've found using the addScreenPositionFunction() on a p5.Graphics object works fine. But I don't know if this is universal. Anyways here's an example sketch if you think it's worth adding to the documentation.

let graphics;

function setup() {
  createCanvas(400, 400);
  graphics = createGraphics(width, height, WEBGL);
  addScreenPositionFunction(graphics);
  
  rectMode(CENTER)
  graphics.rectMode(CENTER);
}

function draw() {
  background(220);
  
  graphics.clear();
  graphics.rotateX(0.010);
  graphics.rotateY(0.011);
  graphics.rotateZ(0.012);
  graphics.square(0, 0, 200);
  image(graphics, 0, 0);
  
  push();
  translate(width/2, height/2);
  const gPoint = graphics.screenPosition(100, 0, 0);
  fill(0, 0, 255);
  square(gPoint.x, gPoint.y, 20);
  pop();
}

By the way thanks for making this library it's been super useful!

Correspondence of x,y co-ordinate of 3D cube into x,y co -ordinate in 2D canvas ?

Hello , thanks for this library implementation. I had a doubt on the implementation with regards to finding corresponding co-ordinates in the 2D canvas. In the sketch below , I have used p5js screen position as suggested. The idea is to have a new cube appear when the x, y position of the mouse is inside the cube. (that is within the x,y co -ordinate of the cube) . However , i am not able to achieve that , I also reset the center position to (0,0,0) for a new co-ordinate system . I am wondering if you give me some pointers on this . The link to the sketch is here Sketch

giving different results with the same vector values.

i have used your code of getting the screenX and y positions but instead of giving me the same result for the same X,Y,Z ?
the way i implemented the code is i used only the addScreenPositionFunction function
so what seems to be the issue ?thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.