Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Consider the HTML code below:

JavaScript
<canvas id="taF" draggable="true" ondrag="canvasDragging(event)">
    No Canvas Support!
</canvas>


And this is `canvasDragging` and `context.translate` codes in JavaScript:

JavaScript
// Canvas Context

let contextX = 1000, contextY = 0;
context.translate( contextX, contextY );


// Dragging

const canvasDragging = ( event ) => {

    contextX = event.offsetX;
    contextY = event.offsetY;

}


I have checked the `event` and its properties but when I drag the canvas, the canvas isn't being translated and changed by its viewport.

Why?

What I have tried:

I think the code above is sufficient but it may not.
Posted
Updated 12-Sep-21 23:50pm

1 solution

The translate method adds a translation transform to the current matrix:
CanvasRenderingContext2D.translate() - Web APIs | MDN[^]

It does not bind that transform to the variables you pass in. If you later change the value stored in those variables, the transform will not be changed.

Transformations - Web APIs | MDN[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900