本文摘自PHP中文网,作者黄舟,侵删。
这篇文章主要教大家如何使用Html5编写属于自己的画画板,进行绘画、调整颜色等操作,感兴趣的小伙伴们可以参考一下最近了解到html5强大的绘图功能让我惊奇,于是,写了个小玩意---涂鸦板,能实现功能有:画画,改色,调整画笔大小
html5的绘图可以分为点,线,面,圆,图片等,点和线,这可是所有平面效果的基点,有了这两个东西,没有画不出来的东西,只有想不到的算法。
先上代码了:
html
1 2 3 4 5 6 | < body style = "cursor:pointer" >
< canvas id = "mycavas" width = "1024" height = "400" style = "border:solid 4px #000000" ></ canvas >
< input type = "color" id = "color1" name = "color1" />
< output name = "a" for = "color1" onforminput = "innerHTML=color1.value" ></ output >
< input type = "range" name = "points" id = "size" min = "5" max = "20" />
</ body >
|
效果:

好了,一个简陋的画图界面就搞好啦,下面开始写一些画线的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | $.Draw = {};
$.extend($.Draw, {
D2: "" ,
CX: "" ,
Box: "mycavas" ,
BoxObj: function (){
this .CX=document.getElementById( this .Box);
},
D2: function (){
this .D2 = this .CX.getContext( "2d" );
},
Cricle: function (x, y, r, color) {
if ( this .D2) {
this .D2.beginPath();
this .D2.arc(x, y, r, 0, Math.PI * 2, true );
this .D2.closePath();
if (color) {
this .D2.fillStyle = color;
}
this .D2.fill();
}
},
init: function () {
this .BoxObj();
this .D2();
}
})
|
相信这里的简单代码大家都看得懂,主要就是创建了一个对象,包含创建画布,创建2d对象,画圆方法,和对象初始化方法。
接下里前台html页面来调用这个对象/p>
看代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | var color = "#000000" ;
var size = 5;
document.getElementById( 'color1' ).onchange = function () {
color = this .value;
};
document.getElementById( 'size' ).onchange = function () {
size = this .value;
};
$.Draw.init();
var tag = false ;
var current = {};
document.onmousedown = function (option) {
current.x = option.x;
current.y = option.y;
$.Draw.Cricle(option.x, option.y, size, color);
tag = true ;
}
document.onmouseup = function () {
tag = false ;
}
document.onmousemove = function (option) {
if (tag) {
if (size >= 0) {
$.Draw.Cricle(option.x, option.y, size, color);
}
}
}
|
这段代码主要有如下几个意思
1.捕获颜色空间和拖动条控件的change事件,从而获取对应的颜色和尺寸的数值,存储下来供下面画线用
2.初始化画图对象
3.捕获鼠标的按下,抬起和移动事件,关键在一个开关可以控制油墨
好了,一个简单的涂鸦板就好了,上我的书法:

相关文章:
基于纯CSS3的6种手绘涂鸦按钮效果
如何使用html5与css3完成google涂鸦动画
基于javascript html5 canvas实现可调画笔颜色/粗细/橡皮的涂鸦板
以上就是Html5简单实现涂鸦板的示例代码的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
html5仿微信聊天界面和朋友圈代码
html5简单实现涂鸦板的示例代码
dreamweaver怎么使用标签及代码设计表格?_dreamweaver教程_网页制作
关于html中的代码注释
判断登陆是否失效代码
在web项目中错误代码整理
安全编程之android apk打包代码混淆(代码实例)
html5实现文字轮滚的示例代码
html2canvas把div保存高清图的方法代码
用html5绘制折线图的实例代码
更多相关阅读请进入《涂鸦》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » Html5简单实现涂鸦板的示例代码