Javascript HTML5 Canvas实现的一个画板
作者:suvllian 时间:2024-04-10 10:39:36
本文实例为大家分享了HTML5 Canvas实现的一个画板代码,供大家参考,具体内容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>DEMO6:自定义画板</title>
</head>
<body>
<canvas id="canvas" width="600" height="300">
浏览器不支持canvas <!-- 如果不支持会显示这段文字 -->
</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;" 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: white;" onclick="copyimage();">EXPORT</button>
<br/>
<img src="" id="image_png" width="600px" height="300px">
<br/>
<script type="text/javascript">
var canvas = document.getElementById('canvas'); //获取标签
var ctx = canvas.getContext("2d");
var 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>
标签:js,HTML5,Canvas,画板
0
投稿
猜你喜欢
Django RBAC权限管理设计过程详解
2021-09-02 03:57:07
如何基于Python实现数字类型转换
2023-10-08 00:57:13
python实现websocket的客户端压力测试
2023-09-30 11:19:57
python中wordcloud安装的方法小结
2022-07-11 04:29:44
Python Tkinter之事件处理详解
2021-06-08 07:19:53
python GUI库图形界面开发之PyQt5树形结构控件QTreeWidget详细使用方法与实例
2023-04-12 03:34:40
Python ORM框架Peewee用法详解
2022-11-08 03:00:24
讲解如何利用 Python完成 Saga 分布式事务
2021-08-19 00:03:48
Python字典的核心底层原理讲解
2022-03-26 08:31:09
Python进阶学习之特殊方法实例详析
2022-03-03 22:15:04
Python网络编程之TCP与UDP协议套接字用法示例
2023-12-07 06:34:45
Python还能这么玩之用Python做个小游戏的外挂
2022-12-11 18:27:43
PHP使用flock实现文件加锁的方法
2023-10-29 21:26:59
SQL面试题:求时间差之和(有重复不计)
2024-01-22 00:09:14
python中getaddrinfo()基本用法实例分析
2023-03-15 12:17:34
Falsk 与 Django 过滤器的使用与区别详解
2023-08-21 08:06:02
Python类的基本写法与注释风格介绍
2023-01-15 07:53:52
Python后台开发Django会话控制的实现
2022-11-09 22:29:16
关于计算到期时间的问题
2009-05-26 15:51:00
python进度条显示之tqmd模块
2021-09-29 22:44:36