Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

move canvas element to mouse

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="578" height="200"></canvas>
    <script>
      function writeMessage(canvas, message) {
        var context = canvas.getContext('2d');
        context.clearRect(0, 0, canvas.width, canvas.height);
        context.font = '18pt Calibri';
        context.fillStyle = 'black';
        context.fillText(message, 10, 25);
      }
      function getMousePos(canvas, evt) {
        var rect = canvas.getBoundingClientRect();
        return {
          x: evt.clientX - rect.left,
          y: evt.clientY - rect.top
        };
      }
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');

      canvas.addEventListener('mousemove', function(evt) {
        var mousePos = getMousePos(canvas, evt);
        var message = 'Mouse position: ' + mousePos.x + ',' + mousePos.y;
        writeMessage(canvas, message);
      }, false);
    </script>
  </body>
</html>
      
Comment

PREVIOUS NEXT
Code Example
Javascript :: appending an element to another element javascript 
Javascript :: CREATE A BUTTON THAT INCREMENTS A COUNTER WHEN CLICKED 
Javascript :: click counter in javascript 
Javascript :: foreach break js 
Javascript :: fetch Response object get content type 
Javascript :: what is 5+5 
Javascript :: contains() js 
Javascript :: how to access dictionary keys in js 
Javascript :: How to get current time zone in javascript 
Javascript :: how to unban in discord js 
Javascript :: multiple case switch javascript 
Javascript :: email regular expression javascript 
Javascript :: async import javascript 
Javascript :: js undici fetch data 
Javascript :: get element by id jqueryt 
Javascript :: convert currency to string javascript 
Javascript :: link href javascript 
Javascript :: how to set emmet for jsx in visual studio code 
Javascript :: can you call a function within a function javascript 
Javascript :: angular call child method from parent 
Javascript :: tsconfig 
Javascript :: html2pdf example angular 
Javascript :: tailwind content for nextjs 
Javascript :: what is normalize in javascript 
Javascript :: copy to clipboard jquery example 
Javascript :: sfc in react 
Javascript :: javascript get focusable elements 
Javascript :: javascript progress of xml http request 
Javascript :: upload image to firebase 
Javascript :: select field in react native 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =