banner



how to draw with a mouse

Drawing on the HTML5 Canvas with a Mouse

How to Use the Canvas every bit an Art Tool

Andrew Udell

Photo past Justyn Warner on Unsplash

Setting up the HTML

          <!DOCTYPE html>
<html>
<head>
<Title>Sketch-A-Thing</Championship>
<link rel="stylesheet" type="text/css" href="sketch.css" media="screen"/>
</head>
          <body>                      <!-- Create a Header -->
<div>
<middle>
<h1>Sketch-A-Thing!</h1>
</center>
</div>
<!-- Create the Canvas Element -->
<div>
<center>
<canvas id = "sketchCanvas" width = "900" summit = "450"></canvas>
</center>
</div>
                      <div>
<middle>
<label for="penColor">Pen Color:</label>
<input type="color" id="penColor" value="#000000">
<label for="lineWidth">Pen Width:</label>
<input type="range" min="one" max="ten" value="2" id="lineWidth">
</centre>
</div>
</trunk>
          <script src="sketch.js"></script>            
</html>

CSS Notes

          torso {
font-family: Verdana, sans-serif;
}
canvas {
border-style: solid;
edge-width: 2px;
border-radius: 8px;
margin: 0;
}

A picture of the canvas with the controls set up

This volition be the primary interface users will collaborate with. Figure produced past writer.

Drawing with JavaScript

          // Create a class for the canvas, including attributes for the id, // draw boolean, and coordinates
course Canvas {
constructor(canvas_id, draw, coord) {
this.canvas_id = canvas_id;
this.describe = draw;
this.coord = coord
}
                      // Sets draw to truthful and gets the mouse coordinates on the effect
startDrawing(issue) {
this.draw = true;
let canvas = document.getElementById(this.canvas_id);
this.coord.x = (event.clientX - canvas.getBoundingClientRect().left);
this.coord.y = (result.clientY - canvas.getBoundingClientRect().meridian);
}
                      // The bodily office that will draw on event (mousemove)
drawSketch(event) {
// If the depict boolean isn't truthful, terminate the office
early
if (!this.draw) {
render 0;
}
                      // Select the canvas and get its context
let canvas = certificate.getElementById(this.canvas_id);
let ctx = sail.getContext('2d');
                      // Start the line
ctx.beginPath();

// Pull from the colour and width from the associated
// controls. The line cap is hardcoded to be rounded,
// because it looks more natural for a drawing application
ctx.strokeStyle = certificate.getElementById("penColor").value;
ctx.lineWidth = document.getElementById("lineWidth").value;
ctx.lineCap = "rounded";

                      // Start moving to the coordinates adamant by mouse            
// movement. The position is updated as the cursor moves
ctx.moveTo(this.coord.x, this.coord.y);
this.coord.ten = (outcome.clientX - canvass.getBoundingClientRect().left);
this.coord.y = (event.clientY - canvass.getBoundingClientRect().superlative);

// Specify where the line ends
ctx.lineTo(this.coord.x , this.coord.y);

// Draw the line
ctx.stroke();
}

                      // Sets draw to imitation on event (mouseup)
stopDrawing(upshot) {
this.draw = false;
}
}
          // Write the primary function that will execute on load
function main() {
// Initialize the main object, canvass
let sheet = new Canvas("sketchCanvas", imitation, {10:0 , y:0});
                      // Add together effect listeners for the mousedown, mouseup, and            
// mousemove.
document.addEventListener('mousedown', function(e){
canvass.startDrawing(due east);
});
document.addEventListener('mouseup', function(eastward){
canvas.stopDrawing(e);
});
document.addEventListener('mousemove', role(e){
canvass.drawSketch(e);
});
}
          // Look until the page is loaded before applying the main part
window.addEventListener("DOMContentLoaded", ()=>{
main();
});

Concluding Result

A demo of the canvas by drawing a smiling face

Video Produced by Writer.

A demonstration of the color select and line width by drawing a purple circle and rectangle

Video Produced by Writer

Conclusions

Source: https://levelup.gitconnected.com/drawing-on-the-html5-canvas-with-a-mouse-d92c7d57fb7c

Posted by: hamiltonprionat.blogspot.com

0 Response to "how to draw with a mouse"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel