<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>使用canvas制作画板</title>
<link rel="stylesheet" href="">
<style>
body {
background: #efe;
padding-top: 50px;
} #paint {
border: 2px solid hsl(107, 98%, 56%);
background: #fff;
float: left;
} #paint:hover {
cursor: crosshair;
} #Container {
width: 800px;
height: 100%;
margin: 0 auto;
} #Container .box {
float: left;
margin: 10px;
} #Container .box button {
color: #606060;
border: 1px solid #b7b7b7;
background: #ededed;
cursor: pointer;
text-shadow: 0 1px 1px rgba(0, 0, 0, .3);
box-shadow: 0 1px 2px rgba(0, 0, 0, .2);
width: 102px;
font-size: 1em;
height: 31px;
margin-right: 20px;
margin-left:20px;
}
#Container .box h3 {
margin-top: 2px;
display: inline-block;
} #Container #image_png {
float: left;
width: 800px;
height: 400px;
border: 2px solid hsl(107, 98%, 56%);
background: #fff;
display: none;
} #colorbox {
width: 365px;
height: 52px;
} #colorbox>li {
width: 50px;
height: 50px;
border: 1px solid rgba(0, 0, 0, 0.06);
list-style: none;
float: left;
} #linewidth li {
display: inline;
font-size: 20px;
}
</style>
</head> <body>
<div id="Container">
<ul id="colorbox">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<canvas id="paint" width="800" height="400"></canvas>
<div class="box">
<button type="button" onclick="copyimage()">点击保存</button>
<h3>滑动改变画笔粗细:</h3>
<input type="range" min="5" max="50" value="50" id="trackBar">
<h4 id="hh" style="display:inline">5</h4>
<button onclick="clearAll(this)" id="clearAll_s">清除画布</button>
</div>
<img src="" id="image_png">
</div>
<script src="js/jquery-2.0.0.min.js"></script>
<script>
var mycanvas = document.getElementById("paint");
var ctx = mycanvas.getContext("2d");
ctx.strokeStyle = "red";
var flag = false;
$("#paint").mousedown(function(e) { //当鼠标按下时
var mouseX = e.pageX - this.offsetLeft;
var mouseY = e.pageY - this.offsetTop;
ctx.moveTo(mouseX, mouseY);
flag = true;
});
$("#paint").mousemove(function(e) { //当鼠标抬起时
if (flag == true) {
var mouseX = e.pageX - this.offsetLeft;
var mouseY = e.pageY - this.offsetTop;
ctx.lineTo(mouseX, mouseY); ctx.stroke();
}
});
$("#paint").mouseup(function(e) { //当鼠标移动时
flag = false;
});
//实现颜色版
var colors = ['#000000', '#ff0000', '#0000ff', '#FF7F24', '#00ffff', '#FFFF00', '#4EEE94'];
var font = [20, 40, 60, 80, 100];
var colorboxs = document.getElementById("colorbox").getElementsByTagName("li"); for (var i = 0; i < colors.length; i++) {
colorboxs[i].style.backgroundColor = colors[i];
}
//为画笔添加颜色
$("li").each(function() {
$(this).bind("click", function() {
ctx.beginPath();
ctx.strokeStyle = this.style.backgroundColor;
});
});
//画笔加粗
var trackBar = document.getElementById("trackBar");
trackBar.value = 5;
trackBar.addEventListener("change", function() {
hh.innerHTML = this.value;
ctx.beginPath();
ctx.lineWidth = this.value;
});
//将图画保存
function copyimage(event) {
var img_png_src = mycanvas.toDataURL("image/png");
document.getElementById("image_png").src = img_png_src;
$("#image_png").css("display", "block")
}
//清除画布
function clearAll() {
ctx.clearRect(0, 0, 880, 400);
ctx.beginPath();
} </script>
</body>
</html> 最后效果:

												

最新文章

  1. Android从网络某个地址下载文件、写入SD卡
  2. WebForm Repeater Response以及 地址栏
  3. MVC5+EF6 入门完整教程十一:细说MVC中仓储模式的应用
  4. OD调试4--绕过nag窗口
  5. lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上
  6. 第二节:Maven的运行机制
  7. OC语法7——内存管理之@property参数
  8. 三个C++资源链接(大量)
  9. 旋转数组中的最小数字,剑指offer,P70 二分查找来实现O(logn)的查找
  10. DataTable的名称要后设置
  11. BZOJ_3503_[Cqoi2014]和谐矩阵_高斯消元
  12. 《C#并发编程经典实例》学习笔记—2.6 任务完成时的处理
  13. P5284 [十二省联考2019]字符串问题
  14. python高级编程读书笔记(一)
  15. 深入详解美团点评CAT跨语言服务监控(七)消息分析器与报表(二)
  16. (转) SQL Server编程系列(1):SMO介绍
  17. main.cpp
  18. eos交易同步过程和区块生产过程源码分析
  19. 解决iPad/iPhone下手机号码会自动被加上a标签的问题
  20. C#.NET下转换泛型列表为JSON格式

热门文章

  1. CentOS 7配置LNMP开发环境及配置文件管理
  2. UWP开发必备以及常用知识点总结
  3. PHP之使用网络函数和协议函数
  4. 读书笔记汇总 - SQL必知必会(第4版)
  5. C++随笔:.NET CoreCLR之GC探索(2)
  6. C# 程序中嵌入百度地图
  7. SSH框架和Redis的整合(2)
  8. Unable to create the selected property page. An error occurred while automatically activating bundle net.sourceforge.pmd
  9. windows 7(32/64位)GHO安装指南(序篇)~
  10. excel常用技巧