Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your x and y points will still be 0 and -70 as they are relative to the translation (rotation). It basically means you would need to "reverse engineer" the matrix to get the resulting position you see on the canvas.</p> <p>If you want to calculate a line which goes 70 pixels at -10 degrees you can use simple trigonometry to calculate it yourself instead (which is easier than going sort of backwards in the matrix).</p> <p>You can use a function like this that takes your context, the start position of the line (x, y) the length (in pixels) and angle (in degrees) of the line you want to draw. It draw the line and returns an object with <code>x</code> and <code>y</code> for the end point of that line:</p> <pre><code>function lineToAngle(ctx, x1, y1, length, angle) { angle *= Math.PI / 180; var x2 = x1 + length * Math.cos(angle), y2 = y1 + length * Math.sin(angle); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.stroke(); return {x: x2, y: y2}; } </code></pre> <p>Then just call it as:</p> <pre><code>var pos = lineToAngle(ctx, 0, 0, 70, -10); //show result of end point console.log('x:', pos.x.toFixed(2), 'y:', pos.y.toFixed(2)); </code></pre> <p>Result:</p> <pre><code>x: 68.94 y: -12.16 </code></pre> <p>Or you can instead extend the canvas' context by doing this:</p> <pre><code>if (typeof CanvasRenderingContext2D !== 'undefined') { CanvasRenderingContext2D.prototype.lineToAngle = function(x1, y1, length, angle) { angle *= Math.PI / 180; var x2 = x1 + length * Math.cos(angle), y2 = y1 + length * Math.sin(angle); this.moveTo(x1, y1); this.lineTo(x2, y2); return {x: x2, y: y2}; } } </code></pre> <p>And then use it directly on your context like this:</p> <pre><code>var pos = ctx.lineToAngle(100, 100, 70, -10); ctx.stroke(); //we stroke separately to allow this being used in a path console.log('x:', pos.x.toFixed(2), 'y:', pos.y.toFixed(2)); </code></pre> <p>(0 degrees will point to the right).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload