This article introduces the JS implementation of a simple canvas drawing instance code, the need for friends can refer to the
To define a variable:
[JavaScript]
Copy Code code as follows:
var startx;
var starty;
var EndX;
var EndY;
var radius;
var canvas = document.getElementById ("MyCanvas");
var context = Canvas.getcontext ("2d");
var select = document.getElementsByTagName ("select");
var startx;
var starty;
var EndX;
var EndY;
var radius;
var canvas = document.getElementById ("MyCanvas");
var context = Canvas.getcontext ("2d");
var select = document.getElementsByTagName ("select");
function Section:
[JavaScript]
Copy Code code as follows:
window.onload=function () {
Canvas.onmousedown = function (e) {
e = e | | Event
startx = E.clientx;
starty = E.clienty;
if (Select[0].value = = "Arc") {
canvas.onmousemove = Moveshowarc;
} else {
canvas.onmousemove = Moveshowrect;
}
}
canvas.onmouseup = function () {
canvas.onmousemove = "";
}
}
function Moveshowrect (e) {
context.clearrect (0, 0, 500, 300);
endx = E.clientx-startx;
EndY = E.clienty-starty;
Context.beginpath ();
Context.rect (StartX, Starty, EndX, EndY);
Context.fillstyle = "#8ED6FF";
Context.fill ();
context.linewidth = 3;
Context.strokestyle = "BLACK";
Context.stroke ();
}
function Moveshowarc (e) {
context.clearrect (0, 0, 500, 300);
endx = E.clientx-startx;
EndY = E.clienty-starty;
radius = math.sqrt (Math.pow (endx,2) +math.pow (endy,2));
Context.beginpath ();
Context.arc (startx, starty,radius,0,2 * math.pi,false);
Context.fillstyle = "#8ED6FF";
Context.fill ();
context.linewidth = 3;
Context.strokestyle = "BLACK";
Context.stroke ();
}
window.onload=function () {
Canvas.onmousedown = function (e) {
e = e | | Event
startx = E.clientx;
starty = E.clienty;
if (Select[0].value = = "Arc") {
canvas.onmousemove = Moveshowarc;
} else {
canvas.onmousemove = Moveshowrect;
}
}
canvas.onmouseup = function () {
canvas.onmousemove = "";
}
}
function Moveshowrect (e) {
context.clearrect (0, 0, 500, 300);
endx = E.clientx-startx;
EndY = E.clienty-starty;
Context.beginpath ();
Context.rect (StartX, Starty, EndX, EndY);
Context.fillstyle = "#8ED6FF";
Context.fill ();
context.linewidth = 3;
Context.strokestyle = "BLACK";
Context.stroke ();
}
function Moveshowarc (e) {
context.clearrect (0, 0, 500, 300);
endx = E.clientx-startx;
EndY = E.clienty-starty;
Radius = math.sqrt (Math.pow (endx,2) +math.pow (endy,2));
Context.beginpath ();
Context.arc (startx, starty,radius,0,2 * math.pi,false);
Context.fillstyle = "#8ED6FF";
Context.fill ();
context.linewidth = 3;
Context.strokestyle = "BLACK";
Context.stroke ();
}