前段时间,在对H5的回顾中突然对canvas有了感觉,闲来无事便对其进行了一些捯饬。这不,上周我还做了一个好玩的画板呢,废话不多说,直接上代码(PS:翠花,上代码~):

HTML部分:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Canvas 制作画板</title>
<style>
h1{
text-align: center;
margin: 0 auto;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.drawBox{
width: 830px;
height: auto;
margin: auto;
background: #999;
border-radius: 10px;
}
#myCanvas{
padding: 0;
background: #fff;
display: block;
cursor: pointer;
border-radius: 10px;
border: 15px solid #999;
}
.btnBox{
color: #fff;
}
.btnBox>a{
color:#fff;
width: 85px;
line-height: 40px;
text-align: center;
text-decoration: none;
display: inline-block;
background: #666;
border-radius: 10px;
}
.btnBox>a:first-child{
margin: 0 0 12px 15px;
}
.btnBox>a#toolBtn{
width: 50px;
}
.btnBox>input#color{
width: 22px;
height: 25px;
cursor: pointer;
outline: none;
border-radius: 50%;
margin: auto 10px;
}
.btnBox>input#thick{
outline: none;
width: 20px;
padding: 3px;
border-radius: 5px;
}
.btnBox>input#range{
outline: none;
position: relative;
top: 5px;
}
</style>
</head>
<body>
<h1>Canvas 制作画板</h1>
<div class="drawBox">
<canvas id="myCanvas" width="800" height="600">
对不起,您的浏览器不兼容canvas标签!
</canvas>
<div class="btnBox">
<a href="javascript:;" id="toolBtn" title="选择橡皮">橡皮</a>
<a href="javascript:;" id="clearBtn" title="清除画板">清除画板</a>
<a href="javascript:;" id="downLoad" title="点击下载">下载图片</a>
<input type="color" id="color" title="选择画笔颜色"/>
<input type="text" id="thick" value="1" title="请输入画笔粗细值"/>
细 <input type="range" name="range" id="range" title="画笔粗细调整" value="1" min="1" max="50"/> 粗
</div>
</div>
</body>
</html>

HTML页面准备就绪,Javascript部分:

document.onload=function(){
var can = document.getElementById('myCanvas'),
downBtn = document.getElementById('downLoad'),
clearBtn = document.getElementById('clearBtn'),
colorBtn = document.getElementById('color'),
rangeBtn = document.getElementById('range'),
toolBtn = document.getElementById('toolBtn'),
thick = document.getElementById('thick');// 获取按钮等元素
var ctx = can.getContext('2d');// 定义一个canvas画布 colorBtn.addEventListener('change',function () {
ctx.strokeStyle=this.value;
});// 颜色改变
rangeBtn.addEventListener('change',function () {
ctx.lineWidth=this.value;
thick.value=this.value;
});// 笔画粗细(拖拽)
thick.addEventListener('blur',function () {
ctx.lineWidth=this.value;
rangeBtn.value=this.value;
});// 笔画粗细(数值输入) toolBtn.addEventListener('click',function () {
if(this.text=='橡皮'){
this.text='画笔';
ctx.strokeStyle='#fff';
ctx.lineWidth=20;
}else{
this.text='橡皮';
ctx.strokeStyle='#000';
ctx.lineWidth=rangeBtn.value;
}
});// 橡皮与画笔的一个转换 // 开始绘制
can.addEventListener('mousedown',function (from) {
// 绘制
var formX=from.clientX-can.offsetLeft-13,
formY=from.clientY-can.offsetTop-13;
ctx.beginPath();
ctx.moveTo(formX,formY); // 画笔移动
function move(to) {
var toX=to.clientX-can.offsetLeft-13,
toY=to.clientY-can.offsetTop-13;
ctx.lineTo(toX,toY);
ctx.stroke();
};
can.addEventListener('mousemove',move); // 停止绘制
document.addEventListener('mouseup',function () {
can.removeEventListener('mousemove',move,false);
});
}); // 下载图片按钮
downBtn.addEventListener('click',function () {
this.href=can.toDataURL('image/png');
this.setAttribute('download','picture.png');
}); // 清除画板按钮-画布情况
clearBtn.addEventListener('click',function () {
ctx.clearRect(0,0,can.width,can.height);
});
}

  在此,我对橡皮的处理可能不够理想,因为如果下载的是透明的,那么会看见橡皮的痕迹,在此我就不对此给出解决的办法,欢迎大家积极探索吧。好的,这一切就绪后,再来一个效果图:

一个基本画板,有兴趣的朋友可以再对细节进行处理以及一些功能的扩展~

用canvas画布画一个画板的更多相关文章

  1. Canvas 如何画一个四分之一圆

    转: Canvas 如何画一个四分之一圆 HTML: Document JS: var c = document.getElementById('ctx') var ctx = c.getContex ...

  2. 为了让她学画画——熬夜用canvas实现了一个画板

    前言 大家好,我是Fly, canvas真是个强大的东西,每天沉迷这个无法自拔, 可以做游戏,可以对图片处理,后面会给大家分享一篇,canvas实现两张图片找不同的功能, 听着是不是挺有意思的, 有点 ...

  3. canvas画布——画八卦图

    实例 创建一个圆形: var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d") ...

  4. canvas之画一个三角形

    <canvas id="canvas" width="500" height="500" style="background ...

  5. [技术博客]海报图片生成——小程序canvas画布

    目录 背景介绍 canvas简介 代码实现 难点讲解 圆角矩形裁剪失败之PS的妙用 编码不要过硬 对过长的文字进行截取 真机首次生成时字体不对 drawImage只能使用本地图片 背景介绍 目标:利用 ...

  6. canvas绘画基础(一):认识canvas画布

    html5提供了一个<canvas>标签,结合javascript的canvas api接口可以用来绘制图形和动画.最近工作中涉及到画图的任务,下面来了解一下canvas的基础:canva ...

  7. 用canvas画一个的小画板(PC端移动端都能用)

    前言 本篇的内容主要包括: canvas标签简介 画板的功能简介 画板的JS部分(包括:1.获取画布 2.使画板全屏幕显示且自适应 3.如何绘制直线 4.绘画时的三种状态(鼠标点击.移动.离开)5.画 ...

  8. 使用Canvas和JavaScript做一个画板

    本文同步于个人博客:https://zhoushuo.me/blog/2018/03/11/drawing-borad/ 前些天学习了HTML5的<canvas>元素,今天就来实践一下,用 ...

  9. 使用H5 canvas画一个坦克

      具体步骤如下:   1. 首先做出绘图区,作为坦克的战场   <canvas id="floor" width="800px" height=&quo ...

随机推荐

  1. C#调用Oracle带输出数据集的存储过程

    1.创建一个带输出数据集的Oracle存储过程 create or replace procedure PRO_test(in_top in number,cur_out out sys_refcur ...

  2. JAVA中方法和变量在继承中的覆盖和隐藏(一)

    我们知道,在JAVA中,子类可以继承父类,如果子类声明的方法与父类有重名的情况怎么办,大伙儿都知道要是重写,但是实际上这又分为两种情况,就是方法和变量在继承时的覆盖和隐藏问题,这些概念性的东西看似无聊 ...

  3. [Z] Windbg以及vs debug使用

    Windbg 一篇中国人写的质量非常高的Windbg文章:篇中国人写的质量非常高的Windbg文章: http://www.yiiyee.cn/Blog/windbg/ code project上的W ...

  4. 「小程序JAVA实战」小程序视频处理工具ffmpeg(47)

    转自:https://idig8.com/2018/09/16/xiaochengxujavashizhanxiaochengxushipinchuligongjuffmpeg46/ 前面已经把视频成 ...

  5. Android 获取图片转bitmap

    1. Resources resources = mContext.getResources(); Drawable drawable = resources.getDrawable(R.drawab ...

  6. mac下的一个类似“_kbhit()”实现

    #include <sys/select.h> #include <termios.h> #include <sys/ioctl.h> int _kbhit() { ...

  7. 一行代码让App运行时iPhone不会进入锁屏待机状态

    转自:http://www.cocoachina.com/iphonedev/sdk/2010/1028/2260.html 如果你不希望应用运行时 iPhone 进入锁屏待机状态,加入下面这行代码即 ...

  8. c# 之Web.config

    Web.config文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式), 它可以出现在应用程序的每一个目录 ...

  9. LUA全总结

    ------------------------------------------------------------------------------ --2018.7.21 do --开启或关 ...

  10. __stdcall详解

    对_stdcall 的理解(上) 在C语言中,假设我们有这样的一个函数:int function(int a,int b) 调用时只要用result = function(1,2)这样的方式就可以使用 ...