首页 技术 正文
技术 2022年11月15日
0 收藏 702 点赞 3,713 浏览 2812 个字
<!DOCTYPE html>
<html><head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>使用canvas制作画板</title>
<link rel="stylesheet" href="" rel="external nofollow" >
<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>最后效果:

使用HTML5的cavas实现的一个画板


    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,361
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,853