将下面的代码改成createjs的方法 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8" /> </head> <body> <canvas id="canvas" width="600" height="300""></canvas><br> <button style="width:80px;background-color:yellow;" onclick='linecolor = "yellow";'>YELLOW</button> <button style="width:80px;background-color:red;" onclick='linecolor = "red";'>RED</button> <button style="width:80px;background-color:blue;" onclick='linecolor = "blue";'>BLUE</button> <button style="width:80px;background-color:green;" onclick='linecolor = "green";'>GREEN</button> <button style="width:80px;background-color:white;" onclick='linecolor = "white";'>WHITE</button> <button style="width:80px;background-color:black;color:white;" onclick='linecolor = "black";'>BLACK</button> <br> <button style="width:80px;background-color:white;" onclick="linw = 4;">4px</button> <button style="width:80px;background-color:white;" onclick="linw = 8;">8px</button> <button style="width:80px;background-color:white;" onclick="linw = 16;">16px</button> <br> <button style="width:80px;background-color:pink;" onclick="copyimage();">EXPORT</button> <br> <img src="" id="image_png" width="600" height="300"> <script type="text/javascript"> var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); //画一个黑色矩形 ctx.fillStyle="black"; ctx.fillRect(0,0,600,300); //按下标记 var onoff = false; var oldx = -10; var oldy = -10; //设置颜色 var linecolor = "white"; //设置线宽 var linw = 4; //添加鼠标移动事件 canvas.addEventListener("mousemove",draw,true); //添加鼠标按下事件 canvas.addEventListener("mousedown",down,false); //添加鼠标弹起事件 canvas.addEventListener("mouseup",up,false); function down(event){ onoff = true; oldx = event.pageX-10; oldy = event.pageY-10; } function up(){ onoff = false; } function draw(event){ if(onoff == true){ var newx = event.pageX-10; var newy = event.pageY-10; ctx.beginPath(); ctx.moveTo(oldx,oldy); ctx.lineTo(newx,newy); ctx.strokeStyle=linecolor; ctx.lineWidth=linw; ctx.lineCap="round"; ctx.stroke(); oldx = newx; oldy = newy; }; }; function copyimage(event){ var img_png_src = canvas.toDataURL("image/png"); document.getElementById("image_png").src = img_png_src; } </script> </body> </html>
时间: 2023-06-11 08:10:28 浏览: 118
以下是使用createjs库改写后的代码:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<script src="https://code.createjs.com/1.0.0/createjs.min.js"></script>
</head>
<body>
<canvas id="canvas" width="600" height="300"></canvas><br>
<button style="width:80px;background-color:yellow;" onclick='linecolor = "yellow";'>YELLOW</button>
<button style="width:80px;background-color:red;" onclick='linecolor = "red";'>RED</button>
<button style="width:80px;background-color:blue;" onclick='linecolor = "blue";'>BLUE</button>
<button style="width:80px;background-color:green;" onclick='linecolor = "green";'>GREEN</button>
<button style="width:80px;background-color:white;" onclick='linecolor = "white";'>WHITE</button>
<button style="width:80px;background-color:black;color:white;" onclick='linecolor = "black";'>BLACK</button>
<br>
<button style="width:80px;background-color:white;" onclick="linw = 4;">4px</button>
<button style="width:80px;background-color:white;" onclick="linw = 8;">8px</button>
<button style="width:80px;background-color:white;" onclick="linw = 16;">16px</button>
<br>
<button style="width:80px;background-color:pink;" onclick="copyimage();">EXPORT</button>
<br>
<img src="" id="image_png" width="600" height="300">
<script type="text/javascript">
// 获取canvas元素
var canvas = document.getElementById("canvas");
// 创建createjs的stage对象
var stage = new createjs.Stage(canvas);
// 画一个黑色矩形
var bg = new createjs.Shape();
bg.graphics.beginFill("black").drawRect(0, 0, canvas.width, canvas.height);
stage.addChild(bg);
stage.update();
// 按下标记
var onoff = false;
var oldx = -10;
var oldy = -10;
// 设置颜色
var linecolor = "white";
// 设置线宽
var linw = 4;
// 添加鼠标移动事件
canvas.addEventListener("mousemove", draw);
// 添加鼠标按下事件
canvas.addEventListener("mousedown", down);
// 添加鼠标弹起事件
canvas.addEventListener("mouseup", up);
function down(event){
onoff = true;
oldx = event.pageX - canvas.offsetLeft;
oldy = event.pageY - canvas.offsetTop;
}
function up(){
onoff = false;
}
function draw(event){
if(onoff == true){
var newx = event.pageX - canvas.offsetLeft;
var newy = event.pageY - canvas.offsetTop;
var shape = new createjs.Shape();
shape.graphics.setStrokeStyle(linw, 'round', 'round');
shape.graphics.beginStroke(linecolor);
shape.graphics.moveTo(oldx, oldy);
shape.graphics.lineTo(newx, newy);
shape.graphics.endStroke();
stage.addChild(shape);
stage.update();
oldx = newx;
oldy = newy;
}
}
function copyimage(event){
var img_png_src = canvas.toDataURL("image/png");
document.getElementById("image_png").src = img_png_src;
}
</script>
</body>
</html>
阅读全文