Change scale position p5 javascript

Caspar B

I have a canvas with an ellipse in it. I can then compare the mouse X and mouse Y to the ellipse X and the ellipse Y for a fake, but valid, mouse 'hover' test. When I hover over the shape the canvas is scaled to twice the size as it was before, and the shape appears larger. However, the scale always remains at the coordinates 0,0 i.e. top left. Is there any way to change the coordinates at which the canvas is scaled at? This way, I would be able to alter the program so that, when hovering over the shape, the new canvas would scale so that the top left is my mouse position (I can move the position of the scale). I've written a little example to demonstrate where I am, currently.

function setup(scaleValue) {
// create canvas and scale to value
  var canvas = createCanvas(300,200);
  scale(scaleValue);
  background(51);
  
// create ellipse
  fill(255,255,255,100);
  stroke(255,255,255);
  ellipse(30,30,40,40);
}
function draw() {
// get mouse X and Y and create an X and Y value for the ellipse
  var x = mouseX;
  var y = mouseY;
  this.position = createVector(30,30);
  
// check for mouse hover by comparing mouse X and Y to ellipse X and Y
  if (Math.abs(mouseX - this.position.x) < 30 && Math.abs(mouseY - this.position.y) < 30) {
    setup(2);
  }
  if (Math.abs(mouseX - this.position.x) > 30 && Math.abs(mouseY - this.position.y) > 30) {
    setup(1);
  }
}
<head>
  <script src="//cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.10/p5.js"></script>
</head>
<body>
</body>

Kevin Workman

You can use the translate() function to move the origin. If you do translate(100, 100) for example, the origin will now be at 100,100 and anything you draw at 0,0 will now be drawn at 100,100.

More info can be found in the reference.

Also, why don't you just use the distance formula, or better yet the dist() function, to check whether the mouse is inside the circle?

Again, more info can be found in the reference.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript P5 change a variable after mouseclick event

From Dev

Javascript - Problems to change the Position

From Dev

HTML5 Canvas Scale position

From Dev

javascript change position depending on number

From Dev

Change position of an option using JavaScript

From Dev

javascript change position depending on number

From Dev

How to change the position of audio in JavaScript?

From Dev

Change image with javascript mouse position?

From Dev

Function that filters what is shown using p5 and Javascript

From Dev

Button won't hide in p5 javascript

From Dev

Javascript : accurate CSS TranslateX ( position ) taking Transform Scale into account

From Dev

OpenLayers.Map change position of Attribution in JavaScript

From Dev

Javascript conditions to change menu position on scroll down

From Dev

change the value of background position with jquery or javaScript

From Dev

Javascript - setInterval to change element's position

From Dev

Javascript tag not running if i change position

From Dev

javascript: change CSS position by relative amount

From Dev

Javascript Change position of a div and after apply an animation

From Dev

Scale canvas to mouse position

From Dev

Position sticky and scale

From Dev

Scale mesh1, mesh2 automatically change it's position?

From Dev

How to change primefaces p:confirmDialog position

From Dev

How to change primefaces p:confirmDialog position

From Dev

Change Krunner size and position in KDE Plasma 5

From Dev

Change Krunner size and position in KDE Plasma 5

From Dev

HTML5 canvas how to change putImageData scale

From Dev

canvas.scale(scale,scale,px,py) jerks to a new position

From Dev

Scale the position of child elements, but not their size?

From Dev

Scale a circle at same center position

Related Related

  1. 1

    Javascript P5 change a variable after mouseclick event

  2. 2

    Javascript - Problems to change the Position

  3. 3

    HTML5 Canvas Scale position

  4. 4

    javascript change position depending on number

  5. 5

    Change position of an option using JavaScript

  6. 6

    javascript change position depending on number

  7. 7

    How to change the position of audio in JavaScript?

  8. 8

    Change image with javascript mouse position?

  9. 9

    Function that filters what is shown using p5 and Javascript

  10. 10

    Button won't hide in p5 javascript

  11. 11

    Javascript : accurate CSS TranslateX ( position ) taking Transform Scale into account

  12. 12

    OpenLayers.Map change position of Attribution in JavaScript

  13. 13

    Javascript conditions to change menu position on scroll down

  14. 14

    change the value of background position with jquery or javaScript

  15. 15

    Javascript - setInterval to change element's position

  16. 16

    Javascript tag not running if i change position

  17. 17

    javascript: change CSS position by relative amount

  18. 18

    Javascript Change position of a div and after apply an animation

  19. 19

    Scale canvas to mouse position

  20. 20

    Position sticky and scale

  21. 21

    Scale mesh1, mesh2 automatically change it's position?

  22. 22

    How to change primefaces p:confirmDialog position

  23. 23

    How to change primefaces p:confirmDialog position

  24. 24

    Change Krunner size and position in KDE Plasma 5

  25. 25

    Change Krunner size and position in KDE Plasma 5

  26. 26

    HTML5 canvas how to change putImageData scale

  27. 27

    canvas.scale(scale,scale,px,py) jerks to a new position

  28. 28

    Scale the position of child elements, but not their size?

  29. 29

    Scale a circle at same center position

HotTag

Archive