Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

draw a rectangle on canvas on pointermove


    let isCon = false;
    let cur = 0;
    const shapes = [];
    const updateFrame = () => {
      shapes.forEach((el) => {
        context.strokeRect(el.x1, el.y1, el.x2 - el.x1, el.y2 - el.y1);
      });
    }

    canvas.addEventListener("pointerdown", function(e){
      isCon = true;
      shapes.push({
        x1: e.clientX - canvas.offsetLeft,
        y1: e.clientY - canvas.offsetTop,
        x2: e.clientX - canvas.offsetLeft,
        y2: e.clientY - canvas.offsetTop,
      });
      context.save();
    });

    canvas.addEventListener("pointermove", function(e){
      if (isCon) {
        context.clearRect(0, 0, canvas.width, canvas.height);
        shapes[cur].x2 = e.clientX - canvas.offsetLeft;
        shapes[cur].y2 = e.clientY - canvas.offsetTop;
        requestAnimationFrame(updateFrame);
      }
    });

    canvas.addEventListener("pointerup", function(){
      isCon = false;
      cur += 1;
    });
Comment

PREVIOUS NEXT
Code Example
Javascript :: H. Nazmul Hassan 
Javascript :: node js check if a user exists in mysql 
Javascript :: javascript round decimal 
Javascript :: shadow border react native 
Javascript :: drupal 8 render node programmatically 
Javascript :: how to clean react native project 
Javascript :: moment js difference between start and end in hours 
Javascript :: lottie delay between loops 
Javascript :: refresh page after delete angular 
Javascript :: javascript canvas dot 
Javascript :: change js to json 
Javascript :: javascript get timestamp 
Javascript :: queryselector aria-label 
Javascript :: how to know type of dom element in js 
Javascript :: how to delete the last part of a string in node js 
Javascript :: vue js store and retrieve api data to localstorage 
Javascript :: react native touchableopacity 
Javascript :: how to format multiline string in javascript 
Javascript :: dropzone add download button 
Javascript :: redirect script javascript 
Javascript :: vehicle number regex 
Javascript :: iso date javascript 
Javascript :: npm json server 
Javascript :: set delay javascript 
Javascript :: react native heroicons 
Javascript :: match any character across multiple line regex 
Javascript :: validatorjs get all errors 
Javascript :: react native class component constructor 
Javascript :: javascript new date 30 days ago 
Javascript :: js date subtract days 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =