javascript

位置:IT落伍者 >> javascript >> 浏览文章

JS实现简单的Canvas画图实例


发布日期:2019年03月28日
 
JS实现简单的Canvas画图实例
这篇文章介绍了JS实现简单的Canvas画图实例代码有需要的朋友可以参考一下

定义变量:
[javascript]

复制代码 代码如下:
var startX;
var startY;
var endX;
var endY;
var radius;
var canvas = documentgetElementById("myCanvas");
var context = canvasgetContext("d");
var select = documentgetElementsByTagName("select");
var startX;
var startY;
var endX;
var endY;
var radius;
var canvas = documentgetElementById("myCanvas");
var context = canvasgetContext("d");
var select = documentgetElementsByTagName("select");


函数部分:
[javascript]

复制代码 代码如下:


windowonload=function() {
canvasonmousedown = function(e) {
e = e || event;
startX = eclientX;
startY = eclientY;
if(select[]value == "arc") {
canvasonmousemove = moveShowArc;
} else {
canvasonmousemove = moveShowRect;
}
}
canvasonmouseup = function() {
canvasonmousemove = "";
}
}
function moveShowRect(e) {
contextclearRect( );
endX = eclientX startX;
endY = eclientY startY;
contextbeginPath();
contextrect(startX startY endX endY);
contextfillStyle = "#EDFF";
contextfill();
contextlineWidth = ;
contextstrokeStyle = "black";
contextstroke();
}
function moveShowArc(e) {
contextclearRect( );
endX = eclientX startX;
endY = eclientY startY;
radius = Mathsqrt(Mathpow(endX)+Mathpow(endY));
contextbeginPath();
contextarc(startX startYradius * MathPIfalse);
contextfillStyle = "#EDFF";
contextfill();
contextlineWidth = ;
contextstrokeStyle = "black";
contextstroke();
}
windowonload=function() {
canvasonmousedown = function(e) {
e = e || event;
startX = eclientX;
startY = eclientY;
if(select[]value == "arc") {
canvasonmousemove = moveShowArc;
} else {
canvasonmousemove = moveShowRect;
}
}
canvasonmouseup = function() {
canvasonmousemove = "";
}
}
function moveShowRect(e) {
contextclearRect( );
endX = eclientX startX;
endY = eclientY startY;
contextbeginPath();
contextrect(startX startY endX endY);
contextfillStyle = "#EDFF";
contextfill();
contextlineWidth = ;
contextstrokeStyle = "black";
contextstroke();
}
function moveShowArc(e) {
contextclearRect( );
endX = eclientX startX;
endY = eclientY startY;
radius = Mathsqrt(Mathpow(endX)+Mathpow(endY));
contextbeginPath();
contextarc(startX startYradius * MathPIfalse);
contextfillStyle = "#EDFF";
contextfill();
contextlineWidth = ;
contextstrokeStyle = "black";
contextstroke();
}

               

上一篇:JS实现点击下载的小例子

下一篇:js简单实例处理url特殊符号&处理