注意部分:


canvas的height和width不能再css中设定,应该在html中设定,否则会影响页面的分辨率。

效果图:


图1:

代码


css:

#canvas{
cursor: crosshair;
}
button{
width: 80px;
}
.yellowBtn{
background-color: yellow;
}
.redBtn{
background-color: red;
}
.blueBtn{
background-color: blue;
}
.greenBtn{
background-color: green;
}
.whiteBtn{
background-color: white;
}
.blackBtn{
background-color:black;
}

页面:

    <canvas id="canvas" width="900" height="400">浏览器不支持html5</canvas>
<br>
<button id="yellow" class="yellowBtn" onclick="lineColor='yellow';">YELLOW</button>
<button id="red" class="redBtn" onclick="lineColor='red';">RED</button>
<button id="blue" class="blueBtn" onclick="lineColor='blue';">BLUE</button>
<button id="green" class="greenBtn" onclick="lineColor='green';">GREEN</button>
<button id="white" class="whiteBtn" onclick="lineColor='white';">WHITE</button>
<button id="black" class="blackBtn" onclick="lineColor='black';">BLACK</button>
<br>
<button class="lineWeight4" class="whiteBtn" onclick="lineWeight=2;">2px</button>
<button class="lineWeight4" class="whiteBtn" onclick="lineWeight=4;">4px</button>
<button class="lineWeight8" class="whiteBtn" onclick="lineWeight=8;">8px</button>
<button class="lineWeight12" class="whiteBtn" onclick="lineWeight=12;">12px</button>

js:

   <script type="text/javascript">
var canvas = document.getElementById("canvas"); //判断是否支持canvas
if(!canvas || !canvas.getContext){
return false;
} //获得context对象,目前只支持2d
var ctx = canvas.getContext("2d"); //画出画板背景,此处为矩形
ctx.fillStyle = "black";
ctx.fillRect(0, 0, 900, 400); //初始化鼠标是否按下和坐标点位置, true为按下
var mouseState = false,
oldX = -10,
oldY = -10,
lineColor = "white",
lineWeight = 2; //canvas添加鼠标事件, 鼠标移动、鼠标按下和鼠标弹起
if(window.addEventListener){
canvas.addEventListener("mousemove", draw, true);
canvas.addEventListener("mousedown", down, false);
canvas.addEventListener("mouseup", up, false); }else{
canvas.attachEvent("onmousemove", draw);
canvas.attachEvent("onmousedown", down);
canvas.attachEvent("onmouseup", up);
} //鼠标按下,拖动画图
function draw(event){
if(mouseState === true){
var newX = event.clientX - 10;
var newY = event.clientY - 10; ctx.beginPath();
ctx.moveTo(oldX, oldY);
ctx.lineTo(newX, newY); ctx.strokeStyle = lineColor;
ctx.lineWidth = lineWeight;
ctx.lineCap = "round";
ctx.stroke(); oldX = newX;
oldY = newY;
} } function down(event){
mouseState = true;
oldX = event.clientX - 10;
oldY = event.clientY - 10;
} function up(){
mouseState = false;
} </script>

增加导出功能:


使用 canvas.toDataURL("image/png");保存图片,eg:

    function exportImage(event){
var imgSrc = canvas.toDataURL("image/png");
document.getElementById("image").src = imgsrc;
}

canvas——画板的更多相关文章

  1. QML学习笔记(二)-纯qml画图实现canvas画板-鼠标画图

    作者: 狐狸家的鱼 Github: 八至 版权声明:如需转载请获取授权和联系作者 用纯qml实现canvas画板功能,用鼠标进行画图,可以画直线,画圆,画矩形,画弧线. 由于canvas画图会有延迟和 ...

  2. 简易的canvas画板

    没事仿照windows画板工具用canvas实现了一个简易版的画板. html: <!doctype html> <html> <head> <meta ch ...

  3. canvas 画板

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. html5 canvas画板

    点击查看演示地址 <!DOCTYPE HTML> <html> <title>HTML5-CanvasDrawDemo</title> <meta ...

  5. html5 canvas 画板

    <!doctype html> <head> <meta http-equiv="Content-Type" content="text/h ...

  6. JS canvas 画板 撤销

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. Canvas画板

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA6QAAAGgCAIAAAAy0z21AAAgAElEQVR4nO3dfYwkZ30n8JqZfcNex8

  8. canvas画板基础应用的学习

    canvas是html5中的绘图容器,我们可以通过javascript的控制来进行图形的绘制,绘制对象可以是路径.盒.圆.字符等,同时,你也可以通过js给画布添加图像,下面来介绍canvas的各种基本 ...

  9. canvas实践小实例一 —— 画板工具

    前面讲了一部分的canvasAPI的基础知识,光看API的介绍确实是很无趣乏味,需要一点可以激发内心的激情的东西来激励自己来学习,于是就了伴随canvasAPI学习的小实例,这样通过API的知识,结合 ...

随机推荐

  1. java web 中的转发和重定向

    假设应用程序的 contextPath 为 /ctx,在 http://localhost:8080/ctx/a/b 资源中,我们转发和重定向到 http://localhost:8080/ctx/x ...

  2. (转)php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法

    php 获取今日.昨日.上周.本月的起始时间戳和结束时间戳的方法,主要使用到了 php 的时间函数 mktime. 下面首先还是直奔主题以示例说明如何使用 mktime 获取今日.昨日.上周.本月的起 ...

  3. Redis介绍

    Redis的介绍 Remote Dictionary Server(Redis)是一个基于 key-value 键值对的持久化数据库存储系统.支持多种数据结构,包括 string (字符串).list ...

  4. C# - linq查询现有的DataTable

    可以通过linq对现有的DataTable进行查询,并将结果拷贝至新的DataTable中例如: // Query the SalesOrderHeader table for orders plac ...

  5. C# - winform使用Dictionary的时候,程序一闪而过!

    概述: 具体也不知道是多线程造成,还是Dictionary的缺陷. 解决方案: 1.如果可能会添加相同键值,你就别用add,直接添加键值,这样不报错 2.使用Try...catch可以捕获异常. 3. ...

  6. iOS消息推送机制

    iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务 ...

  7. 禁用UITextField复制粘贴等方法

    要实现此功能只需创建一个继承自UITextField的子类,重写以下方法即可. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{ ...

  8. Nodejs异步流程控制Async

    http://www.cnblogs.com/huair_12/p/4117351.html 很好的总结 关联下 以便以后学习使用

  9. angularjs应用骨架(2)

    时隔一个星期,接着上一篇的angularjs应用骨架继续聊聊angularjs其他的其他的内容. 区分UI和控制器的职责 在应用控制器中有三种职责: 1.为应用中模型设置初始状态 2.通过$scope ...

  10. jQuery慢慢啃之事件对象(十一)

    1.event.currentTarget//在事件冒泡阶段中的当前DOM元素 $("p").click(function(event) { alert( event.curren ...