这篇文章介绍了JS实现简单的Canvas画图实例代码
有需要的朋友可以参考一下
定义变量:
[javascript]
复制代码 代码如下:
var startX;
var startY;
var endX;
var endY;
var radius;
var canvas = document
getElementById("myCanvas");
var context = canvas
getContext("
d");
var select = document
getElementsByTagName("select");
var startX;
var startY;
var endX;
var endY;
var radius;
var canvas = document
getElementById("myCanvas");
var context = canvas
getContext("
d");
var select = document
getElementsByTagName("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();
}