Canvas - Reset pixels around - javascript

I want to reset pixels to transparent like this:
Consider white as transparent. How to calculate those pixels.
If reset 50% it would be half image, if 100% it would be entire image hidden. On the picture is 35%.
Circle is just for show. It can be any square image.

For each point on the canvas, calculate the angle to your central point. In basic terms:
angle = Math.atan2(y - cy, x - cx);
Where (cx,cy) are the coordinates of your central point, and (x,y) the coordinates of the current pixel being examined.
If this angle is within the range you want to nuke, clear that pixel.
To make this simpler, add Math.PI*3/2, then do % Math.PI*2, and finally multiply by 100/(Math.PI*2) to get a number in the range 0 (up) counter-clockwise to 100.
Now it's a simple case of seeing if that number is less than your target percentage, and if so clear it.

Related

I have an issue when zooming in on a fractal (webgl)

When I zoom in too much and I drag the picture using my mouse it just moves too fast. Similarly if I zoom out too much the picture drags very slowly.
This is how it is zooming in (scale) and how it drags the picture around (xPos, yPos)
fPosition.x = fPosition.x * scale + xPos;
fPosition.y = fPosition.y * scale + yPos;
What should I do to fix this?
You need to adjust the movement by the inverse scale. When at 1 to 1 no zoom you move the image 1 pixel for every one pixel you drag.
When you are zoomed in at 2 to 1. One pixel is now 2 pixels wide (you have a scale of 2) you still want to drag the image at the same screen speed so you move it 1/scale pixels for every pixel you drag. 1/2 = 0.5 for every one pixel you move your mouse/pointer/touch thing .
When zoomed out 0.5 to 1, the scale is now 0.5 and the images is half its size you need to move it 1/scale pixels for every pixel you drag. 1/0.5 = 2
So scale is how big a pixel is and movement is the inverse of that scale. 1 divide scale

How to get pixel position

I feel really dumb asking this, but bear with me. I know about the formula to get the pixel position in a linear array:
pos = (y * width + x) * 4
which works fine. jsFiddle. But before the image/table is linearized, same formula doesn't work. You need to use (let's discard the RGBA for simplicity)
pos = (y-1) * width + x
Why is that? I'm probably missing something really simple.
Update: I knew it was something simple. Silly me.
In javascript pixel coordinates start at 0, the same as any coordinate system. The pixel is referenced by its top left corner, thus the first pixel is at (0,0) , then the next going right (1,0) (2,0) and so on. The pixel below at (0,1) we give coordinates relative to the origin (0,0).
We give sizes as counts. When using width and height they are pixel counts and you start at one when you count. So the 100th pixel is on row 99. Just the same as this is the 21th century but we are in the year 2015.
So no need to subtract 1 from the pixel coordinates.

Drawing a point to point ellipse to allow painting in GUI

I'm implementing some basic annotation draw features, such as arrows. Now I'm a little bit stuck with ellipse.
The methods to draw an ellipse usually address using it's two diameters and eventually a rotation:
However I want to display the ellipse between the point user clicked and the one he's hovering, therefore I need a function that calculates diameters and rotation based on two points:
How would I do that? Can it be achieved with sufficient performance (as it renders during mouse-hovering)?
the steps you shoul follow:
get the angle of the line (from this post: get angle of a line from horizon)
rotate the canvas or at least the part you currently drawing (live demo here: http://www.html5canvastutorials.com/advanced/html5-canvas-transform-rotate-tutorial)
draw an ellipse in canvas (http://www.scienceprimer.com/draw-oval-html5-canvas)
the resulted ellipse will be transformed as described
It can be done in the same way that it is normally done, just using different math to calculate the shape. Without writing the entire code for you, you can start by having an event trigger when the user clicks the mouse button down. The function will copy the users x and y position based on the screen. Then there is a second function which will handle mouse movement. This function will keep track of the x and y coords of the mouse while it is in motion. The final function will be a mouse up event, when a user lifts their finger from the mouse button (assuming this is when the event should be finished). Using the initial and final position of the x and y coordinates, you can calculate the length of the line the user created. That line is the long diameter of the ellipse. Half this number for the large radius. Then use whatever ratio you are using to calculate the smaller radius from the larger one. Then create an ellipse based on these numbers.
For the math: Suppose your first point is x1,y1 and the end point is x2,y2
I'm also assuming that we have a line going from bottom-left to top-right
Distance between two points = sqrt((x2-x1)^2 + (y2-y1)^2) ---> (we will call this d1)
half of this is the length of the large radius ---> (we will call this r1)
Midpoint formula = ((x1+x2)/2 , (y1+y2)/2) ---> axis of rotation (we will call it (m1, m2))
distance from midpoint to end is just the radius
radius is now the hypotenuse of constructed plane, y2-m2 is height of right triangle.
Find the angles between midpoint and one end of larger radius - sin((y2-m2)/r1).
Angle of smaller radius is this angle + pi/4 radians.
calculate length of smaller radius based on ratio.

Calculating smallest rotated rectangle that bounds another and maintains aspect

I need to be able to calculate the size of rectangle 2
To illustrate my problem here is a diagram:
I know the width and height of rectangle 1
I know the aspect ratio of rectangle 2 along with a minimum height and width which will always be larger than rectangle 1
I know the origin of the rotation which is always the centre of rectangle 1
I know the angle of rotation in radians
rectangle 1 must always be fully inside rectangle 2
Given the above variables I need to calculate the smallest size rectangle 2 can be, while maintaining its aspect ratio and rotation origin.
This excellent function calculates the largest possible rectangle within a rotated outer rectangle.
Calculate largest rectangle in a rotated rectangle
I have tried to use it as a base to achieve the behaviour i require but so far with no luck. I'm linking to it in case it is helpful to anyone with greater Math knowledge than I.
Any help would be greatly appreciated.
In the comments you said it's just a trigonometry problem, so I am writing only the formulas.
In your picture, the right bottom triangle will be part of the larger rectangle that you need.
If the smaller angle is x (it's equal to the angle of rotation), and side of inner rectangle is a, then sides of the triangle will be a * cos(x) and a * sin(x). When we move to next side of inner rectangle, the lower b, we will have b * cos(x), b * sin(x).
The picture will be symmetrical, so one side of the larger rectangle will be a * cos(x) + b * sin(x), another will be a * sin(x) + b * cos(x). These are the sizes you need.
You can check with x = 0 (no rotation), x = pi/2 or pi to see what would be the special cases and sizes in those cases.

Straightening a face image within Canvas using click-points

I am trying to build a small app where my users can straighten up a tilted face with just 2 clicks
I ask my users to click on the middle of the nose and the middle of the eyebrows of the face within the image.
From there I get 2 points eyebrowMiddle(x1,y1) and noseMiddle (x2,y2).
Is it possible via these 2 points to calculate how much Canvas
rotation I need to have to rotate the image and make the face straight
in relation to the canvas rectangle?
Also, how can I detect and adjust accordingly if the image is tilted
to the left or right?
Here is a more descriptive image to show you what I mean now.
PS:
x1,y1 and x2,y2 are in relation to the canvas perimeter of
course, not the browser window or anything else.
We have tried the line equation such as m = (x2-x1) / (y2-y1) but the
result is always near 1 so I don't think we are following the right
course at the moment.
We don't care if the image looks wrong in the canvas as long as the
face features are parallel in relation to the bottom of the canvas
(they should be looking straight).
To perform such a rotation, you need to decide of the pivot point. Here i choose the eyebrow.
Then you have to choose a point in the target canvas where this pivot point will be hooked. I decided to choose the point at middle x coordinates, and at fourth of the screen in y.
To compute the rotation angle, you have to use atan2, which will nicely give you the angle for a given deltaY / deltaX in between two points ( angle = Math.atan2 ( delta y , delta x ) ) .
Then to draw :
- Translate to the target point.
- rotate by right angle.
- draw the image centering on its pivot.
ET VOILA, it works :-)
function rotate() {
ctx.save();
// go to default center position
ctx.translate(eyeBrowTargetPosition.x, eyeBrowTargetPosition.y);
// compute angle
var yDelta = noseMiddle.y - eyebrowMiddle.y;
var xDelta = noseMiddle.x - eyebrowMiddle.x ;
var angle = Math.atan2 (yDelta ,xDelta);
// compensate for angle
ctx.rotate(angle);
//draw image centering input on eyebrow
ctx.drawImage(face, -eyebrowMiddle.x, -eyebrowMiddle.y);
ctx.restore();
};
jsbin is here :
http://jsbin.com/wavokaku/2/edit?js,output
result with an approximation of the existing green dots :

Categories