JS实现简单的Canvas画图实例_javascript技巧
程序员文章站
2022-03-11 11:44:41
...
定义变量:
[javascript]
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");
函数部分:
[javascript]
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();
}
[javascript]
复制代码 代码如下:
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");
函数部分:
[javascript]
复制代码 代码如下:
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();
}
推荐阅读
-
JS实现点击链接取消跳转效果的方法_javascript技巧
-
js Li来实现的效果_javascript技巧
-
JS判断文本框内容改变事件的简单实例_javascript技巧
-
js固定DIV高度,超出部分自动添加滚动条的简单方法_javascript技巧
-
js实现鼠标感应向下滑动隐藏菜单的方法_javascript技巧
-
nodejs中简单实现Javascript Promise机制的实例_node.js
-
一个简单的Node.js异步操作管理器分享_javascript技巧
-
javascript 简单抽屉效果的实现代码_javascript技巧
-
js实现点击注册按钮开始读秒倒计时的小例子_javascript技巧
-
js实现div的切换特效上一个下一个_javascript技巧