windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server https://softhier.com/ instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

How to control drawing on canvas using checkbox

Asked By: Anonymous

enter image description hereI am trying to control the drawing on canvas using a checkbox.

If the checkbox is checked the circle should write on the canvas, If the checkbox is unchecked the circle should not write on the canvas.

How can I extend this checkbox to an external source to control the writing and not writing on canvas( i.e. the output from an Arduino etc.)

_x000D_

_x000D_

requestAnimationFrame(animate);
const ctx = canvas1.getContext('2d');
canvas1.width = innerWidth;
canvas1.height = innerHeight;
const bgCan = copyCanvas(canvas1);
const redSize = 10, blueSize = 5; // circle sizes on pixels
const drawSpeed = 2; // when button down draw speed in pixels per frame
var X = 50, Y = 50;
var angle = 0;
var mouseButtonDown = false;
document.addEventListener('mousedown', () => mouseButtonDown = true);
document.addEventListener('mouseup', () => mouseButtonDown = false);
function copyCanvas(canvas) {
    const can = Object.assign(document.createElement("canvas"), {
        width: canvas.width, height: canvas.height
    });
    can.ctx = can.getContext("2d");
    return can;
}
function circle(ctx){
    ctx.fillStyle = 'red';
    ctx.beginPath();
    ctx.arc(X, Y, redSize, 0, Math.PI*2);
    ctx.fill();
}
function direction(ctx){
    const d = blueSize + redSize + 5;
    ctx.fillStyle = 'blue';
    ctx.beginPath();
    ctx.arc(d * Math.sin(angle) + X, d * Math.cos(angle) + Y, blueSize, 0, Math.PI*2);
    ctx.fill(); 
}
function animate(){
    ctx.clearRect(0, 0, ctx.canvas.width,  ctx.canvas.height);
    ctx.drawImage(bgCan, 0, 0);
    if (mouseButtonDown) {
        circle(bgCan.ctx);
        X += Math.sin(angle) * drawSpeed;
        Y += Math.cos(angle) * drawSpeed;
    } else {
        angle += 0.1;
        circle(ctx);
    }
    direction(ctx);
    requestAnimationFrame(animate);   
}

_x000D_

#canvas1{
    position: absolute;
    top:0;
    left: 0;
    width: 100%;
    height: 100%;
}

_x000D_

<canvas id="canvas1"></canvas>

_x000D_

_x000D_

x000D


Solution

Answered By: Anonymous

added one variable checkBoxChecked and it will hold the value of checkbox checked status. and in the animate function added one condition to draw the circle.

if(checkBoxChecked) { circle(bgCan.ctx) };
_x000D_

_x000D_

requestAnimationFrame(animate);
const ctx = canvas1.getContext('2d');
canvas1.width = innerWidth;
canvas1.height = innerHeight;
const bgCan = copyCanvas(canvas1);
const redSize = 10,
  blueSize = 5; // circle sizes on pixels
const drawSpeed = 2; // when button down draw speed in pixels per frame
var X = 50,
  Y = 50;
var angle = 0;
var mouseButtonDown = false;
var checkBoxChecked = false;
document.getElementById("canvas1").addEventListener('mousedown', () => mouseButtonDown = true);
document.getElementById("canvas1").addEventListener('mouseup', () => mouseButtonDown = false);
function copyCanvas(canvas) {
  const can = Object.assign(document.createElement("canvas"), {
    width: canvas.width,
    height: canvas.height
  });
  can.ctx = can.getContext("2d");
  return can;
}

function circle(ctx) {
    ctx.fillStyle = 'red';
    ctx.beginPath();
    ctx.arc(X, Y, redSize, 0, Math.PI * 2);
    ctx.fill();
}

function direction(ctx) {
 
    const d = blueSize + redSize + 5;
    ctx.fillStyle = 'blue';
    ctx.beginPath();
    ctx.arc(d * Math.sin(angle) + X, d * Math.cos(angle) + Y, blueSize, 0, Math.PI * 2);
    ctx.fill();
  
}

function animate() {
  ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  ctx.drawImage(bgCan, 0, 0);
  if (mouseButtonDown) {
    if(checkBoxChecked) { circle(bgCan.ctx) };
    X += Math.sin(angle) * drawSpeed;
    Y += Math.cos(angle) * drawSpeed;
  } else {
    angle += 0.1;
    circle(ctx);
  }
  direction(ctx);
  requestAnimationFrame(animate);
}

function checkBoxClick() {
    checkBoxChecked = document.getElementById("chk").checked;
}

_x000D_

#canvas1 {
  position: absolute;
  top: 30px;
  left: 0;
  width: 100%;
  height: 100%;
}

_x000D_

<input type="checkbox" id="chk" value="test" onclick="checkBoxClick();" />
<label for="chk"> Draw </label>
<canvas id="canvas1"></canvas>

_x000D_

_x000D_

x000D

techinplanet staff


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno