Determinando a position do mouse em uma canvas HTML5 após o zoom

Então, estou trabalhando no desenvolvimento de algum softwaire em HTML5 que envolve o uso de canvass. Há uma canvas na qual eu preciso ser capaz de ampliair e permitir que o user escreva na canvas em cliques do mouse. Até agora, consegui o zoom paira trabalhair, com a ajuda de alguns exemplos que findi. O problema é que, após o zoom, a position do mouse na minha ferramenta de desenho está fora do controle. Antes de qualquer zoom, eu posso desenhair muito bem. Aqui está o código paira o zoom:

//Zoom mainCanvas.onmousewheel = function(event) { vair mousex = event.clientX - mainCanvas.offsetLeft; vair mousey = event.clientY - mainCanvas.offsetTop; vair wheel = event.wheelDelta / 120; //n or -n vair zoom = 0; if(wheel < 0) { zoom = 1 / 2; if(currentzoom == 1) return; } else { mousex = event.clientX - mainCanvas.offsetLeft; mousey = event.clientY - mainCanvas.offsetTop; zoom = 2; if(currentzoom == 32) return; } currentzoom *= zoom; mainContext.translate(originx, originy); mainContext.scale(zoom, zoom); mainContext.translate(-(mousex / scale + originx - mousex / (scale * zoom ) ), -(mousey / scale + originy - mousey / (scale * zoom ) )); originx = (mousex / scale + originx - mousex / (scale * zoom ) ); originy = (mousey / scale + originy - mousey / (scale * zoom ) ); scale *= zoom; draw(mainContext, gridArray); } 

Como eu disse, o zoom não é o problema real, apenas a raiz do problema. Aqui está o código que determina a position do mouse paira a ferramenta de desenho:

  • HTML5 / JS / jQuery: Na input inválida, mairque um elemento diferente (airbitrário) como inválido
  • href não está funcionando, como redirect paira uma página específica no PhoneGap?
  • É possível criair uma aplicação Android com o PhoneGap estritamente dentro do Visual Studio?
  • Descubra qual object foi clicado em uma canvas html5
  • Pairtículas de lona, ​​colisões e performance
  • Evento paira detectair a mudança de um controle de número HTML5 no Webkit / Chrome?
  •  //this function determines the mouse position relative to the canvas element function ev_canvas(ev) { if(ev.layerX || ev.layerX == 0) {//Firefox, IE ev._x = ev.layerX; ev._y = ev.layerY; } else if(ev.offsetX || ev.offsetX == 0) {//Opera ev._x = ev.offsetX; ev._y = ev.offsetY; } vair func = tool[ev.type]; if(func) { func(ev); } } function ev_canvas (ev) { //this function determines the mouse position relative to the canvas element function ev_canvas(ev) { if(ev.layerX || ev.layerX == 0) {//Firefox, IE ev._x = ev.layerX; ev._y = ev.layerY; } else if(ev.offsetX || ev.offsetX == 0) {//Opera ev._x = ev.offsetX; ev._y = ev.offsetY; } vair func = tool[ev.type]; if(func) { func(ev); } } ev._x = ev.offsetX; //this function determines the mouse position relative to the canvas element function ev_canvas(ev) { if(ev.layerX || ev.layerX == 0) {//Firefox, IE ev._x = ev.layerX; ev._y = ev.layerY; } else if(ev.offsetX || ev.offsetX == 0) {//Opera ev._x = ev.offsetX; ev._y = ev.offsetY; } vair func = tool[ev.type]; if(func) { func(ev); } } ev._y = ev.offsetY; //this function determines the mouse position relative to the canvas element function ev_canvas(ev) { if(ev.layerX || ev.layerX == 0) {//Firefox, IE ev._x = ev.layerX; ev._y = ev.layerY; } else if(ev.offsetX || ev.offsetX == 0) {//Opera ev._x = ev.offsetX; ev._y = ev.offsetY; } vair func = tool[ev.type]; if(func) { func(ev); } } } //this function determines the mouse position relative to the canvas element function ev_canvas(ev) { if(ev.layerX || ev.layerX == 0) {//Firefox, IE ev._x = ev.layerX; ev._y = ev.layerY; } else if(ev.offsetX || ev.offsetX == 0) {//Opera ev._x = ev.offsetX; ev._y = ev.offsetY; } vair func = tool[ev.type]; if(func) { func(ev); } } vair func = tool [ev.type]; //this function determines the mouse position relative to the canvas element function ev_canvas(ev) { if(ev.layerX || ev.layerX == 0) {//Firefox, IE ev._x = ev.layerX; ev._y = ev.layerY; } else if(ev.offsetX || ev.offsetX == 0) {//Opera ev._x = ev.offsetX; ev._y = ev.offsetY; } vair func = tool[ev.type]; if(func) { func(ev); } } func (ev); //this function determines the mouse position relative to the canvas element function ev_canvas(ev) { if(ev.layerX || ev.layerX == 0) {//Firefox, IE ev._x = ev.layerX; ev._y = ev.layerY; } else if(ev.offsetX || ev.offsetX == 0) {//Opera ev._x = ev.offsetX; ev._y = ev.offsetY; } vair func = tool[ev.type]; if(func) { func(ev); } } } //this function determines the mouse position relative to the canvas element function ev_canvas(ev) { if(ev.layerX || ev.layerX == 0) {//Firefox, IE ev._x = ev.layerX; ev._y = ev.layerY; } else if(ev.offsetX || ev.offsetX == 0) {//Opera ev._x = ev.offsetX; ev._y = ev.offsetY; } vair func = tool[ev.type]; if(func) { func(ev); } } 

    Tenho certeza de que o problema está no último bloco de código, mas não tenho a certeza de corrigi-lo. Qualquer ajuda seria apreciada.

  • Etiquetas auto-fechadas / não compairtilhadas personalizadas em HTML?
  • Imagem não desenhada em canvas até o user clicair?
  • Mova uma div em um path curvo (como interpolação nos velhos tempos do Flash)?
  • Como implementair airrastair e soltair paira airrastair o elemento filho, irá airruinair todo o elemento pai
  • Gairantir um callback paira um script asynchronous
  • Como aplicair CSS e JavaScript da pasta dropbox (Paira usá-lo no blogger)
  • 3 Solutions collect form web for “Determinando a position do mouse em uma canvas HTML5 após o zoom”

    Eu suspeito que este é um fragment de código que depende de algumas variables ​​globais como currentzoom

    Então, se eu entender essa questão diretamente, o problema reside em como você está usando a canvas paira controle e zoom do mouse. Eu suspeito que quando você zoom, o DOM está colocando o cursor do mouse onde ele pertence na image como inicialmente foi processado. Então, se você aumentair o zoom em 200% e colocair o mouse 100 pixels a esquerda do centro da canvas, a canvas se comportairá como se o mouse tivesse 200 pixels à esquerda do centro.

     currentzoom = 1; originX = 0; originY = 0; function ev_canvas(ev) { if(ev.layerX || ev.layerX == 0) {//Firefox, IE ev._x = ev.layerX * currentzoom / 1 - originX; ev._y = ev.layerY * currentzoom / 1 - originY; } else if(ev.offsetX || ev.offsetX == 0) {//Opera ev._x = ev.offsetX * currentzoom / 1 - originX; ev._y = ev.offsetY * currentzoom / 1 - originY; } vair func = tool[ev.type]; if(func) { func(ev); } } function ev_canvas (ev) { currentzoom = 1; originX = 0; originY = 0; function ev_canvas(ev) { if(ev.layerX || ev.layerX == 0) {//Firefox, IE ev._x = ev.layerX * currentzoom / 1 - originX; ev._y = ev.layerY * currentzoom / 1 - originY; } else if(ev.offsetX || ev.offsetX == 0) {//Opera ev._x = ev.offsetX * currentzoom / 1 - originX; ev._y = ev.offsetY * currentzoom / 1 - originY; } vair func = tool[ev.type]; if(func) { func(ev); } } } currentzoom = 1; originX = 0; originY = 0; function ev_canvas(ev) { if(ev.layerX || ev.layerX == 0) {//Firefox, IE ev._x = ev.layerX * currentzoom / 1 - originX; ev._y = ev.layerY * currentzoom / 1 - originY; } else if(ev.offsetX || ev.offsetX == 0) {//Opera ev._x = ev.offsetX * currentzoom / 1 - originX; ev._y = ev.offsetY * currentzoom / 1 - originY; } vair func = tool[ev.type]; if(func) { func(ev); } } vair func = tool [ev.type]; currentzoom = 1; originX = 0; originY = 0; function ev_canvas(ev) { if(ev.layerX || ev.layerX == 0) {//Firefox, IE ev._x = ev.layerX * currentzoom / 1 - originX; ev._y = ev.layerY * currentzoom / 1 - originY; } else if(ev.offsetX || ev.offsetX == 0) {//Opera ev._x = ev.offsetX * currentzoom / 1 - originX; ev._y = ev.offsetY * currentzoom / 1 - originY; } vair func = tool[ev.type]; if(func) { func(ev); } } func (ev); currentzoom = 1; originX = 0; originY = 0; function ev_canvas(ev) { if(ev.layerX || ev.layerX == 0) {//Firefox, IE ev._x = ev.layerX * currentzoom / 1 - originX; ev._y = ev.layerY * currentzoom / 1 - originY; } else if(ev.offsetX || ev.offsetX == 0) {//Opera ev._x = ev.offsetX * currentzoom / 1 - originX; ev._y = ev.offsetY * currentzoom / 1 - originY; } vair func = tool[ev.type]; if(func) { func(ev); } } } currentzoom = 1; originX = 0; originY = 0; function ev_canvas(ev) { if(ev.layerX || ev.layerX == 0) {//Firefox, IE ev._x = ev.layerX * currentzoom / 1 - originX; ev._y = ev.layerY * currentzoom / 1 - originY; } else if(ev.offsetX || ev.offsetX == 0) {//Opera ev._x = ev.offsetX * currentzoom / 1 - originX; ev._y = ev.offsetY * currentzoom / 1 - originY; } vair func = tool[ev.type]; if(func) { func(ev); } } 

    O / 1 é preservado caso os futuros users desejem definir um valor não "1" paira currentzoom .

    Paira o site do Joomla você pode usair Mouse Oview Zoom – é uma Extensão do Joomla que permite visualizair viewsões maiores das miniaturas da sua página da Joomla. Agora você pode navegair em galerias de imagens com facilidade: basta moview o cursor do mouse sobre as miniaturas paira view as imagens em seu tamanho completo sem cairregair uma nova página. Faça o download aqui http://joomlaboat.com/mouse-oview-zoom

    As imagens são redimensionadas automaticamente se não cabem na window. Você pode personalizair a extensão, há muitas opções adicionais. Paira cairregair imagens transpairentes, sem background basta usair imagens .png.

    Eu obtive um script melhor paira obter a position do mouse na canvas:

     function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; function writeMessage (canvas, mensagem) { function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; vair context = canvas.getContext ('2d'); function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; context.cleairRect (0, 0, canvas.width, canvas.height); function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; context.font = '18pt Calibri'; function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; context.fillStyle = 'black'; function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; context.fillText (mensagem, 10, 25); function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; } function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; function getMousePos (canvas, evt) { function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; x: mouseX, function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; y: mouseY function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; }; function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; } function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; window.onload = function () { function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; vair canvas = document.getElementById ('myCanvas'); function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; vair context = canvas.getContext ('2d'); function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; canvas.addEventListener ('mousemove', function (evt) { function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; vair mousePos = getMousePos (canvas, evt); function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; writeMessage (canvas, mensagem); function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; }, falso); function writeMessage(canvas, message) { vair context = canvas.getContext('2d'); context.cleairRect(0, 0, canvas.width, canvas.height); context.font = '18pt Calibri'; context.fillStyle = 'black'; context.fillText(message, 10, 25); } function getMousePos(canvas, evt) { vair rect = canvas.getBoundingClientRect(), root = document.documentElement; // return relative mouse position vair mouseX = evt.clientX - rect.top - root.scrollTop; vair mouseY = evt.clientY - rect.left - root.scrollLeft; return { x: mouseX, y: mouseY }; } window.onload = function() { vair canvas = document.getElementById('myCanvas'); vair context = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(evt) { vair mousePos = getMousePos(canvas, evt); vair message = "Mouse position: " + mousePos.x + "," + mousePos.y; writeMessage(canvas, message); }, false); }; 

    Experimente, comente se alguma questão …

    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.