02Canvas 画板

1.功能描述

实现画画功能,并可切换画笔颜色、细条粗细。可保存、清空画板。

2.思路

创建画板——》监听鼠标(触屏)事件——》绘制路线

3.具体操作

3.1 创建画板

通过canvas创建画板

<canvas id="xxx" width=300 height=300></canvas>

3.2 监听鼠标(触屏)事件

监听鼠标(触屏)事件,当鼠标按下(触屏)时进行绘画,根据鼠标(手指)移动路线绘制,松开鼠标(手指离屏)时停止绘画。

canvas.ontouchstart = function(aaa){
    var x = aaa.touches[0].clientX;
    var y = aaa.touches[0].clientY;

    using = true;

    if(isEraser){
        context.clearRect(x-5,y-5,10,10);
    } else{
        lastpoint = {"x":x,"y":y};
    }
}

canvas.ontouchmove = function(aaa){
    var x = aaa.touches[0].clientX;
    var y = aaa.touches[0].clientY;

    if(using == false){return;}

    if(isEraser){
        context.clearRect(x-5,y-5,10,10);
    } else{
        var newPoint = {"x":x,"y":y}

        //drawCircle(x,y);
         drawLine(lastPoint.x,lastPoint.y,newPoint.x,newPoint.y);
         lastPoint = newPoint;
    }
}

canvas.ontouchend = function(){
    using = false;
    lastPoint =  {"x":undefined,"y":undefined};
}

3.3 绘制路线

通过点与线的连接,实现绘画功能。

function drawLine(x1,y1,x2,y2){
    context.beginPath();
    context.moveTo(x1,y1);
    context.lineWidth = lineWidth;
    context.lineTo(x2,y2);
    context.stroke();
    context.closePath();
}

4.相关知识点

4.1 js

监听鼠标点击事件

按下去鼠标 document.onmousedown =function(x){console.log(x)}

移动鼠标 document.onmousemove = function(y){console.log(y)}

松开鼠标 document.onmouseup = function(z){console.log(1)}


监听触摸事件(因为手机上没有mouse所以用touch)

开始触摸 canvas.ontouchstart = function(aaa){}

结束触摸 canvas.ontouchend = function(aaa){}

边摸边动 canvas.ontouchmove = function(aaa){}


通过特性检测得知是用mouse还是touch

  • 方法1

    如果设备支持 ontouchstart,结果是null

    如果设备不支持ontouchstart,结果是undefined

  • 方法2

    移动端 ‘ontouchstarts’ in document.body——》true

    手机端 ‘ontouchstarts’ in document.body——》false

    特性检测 document.body.ontouchstart

    触屏设备 canvas.ontouchstart

    非触屏设备 canvas.onmousedown


touch时把当前所有touch事件都存到了touches中,所以从touches[0]中获取坐标


用div画画

首先获取鼠标在视图上的x、y,然后生成1个div,并设置div的样式,最后将div放到画板(div)中。即可打点。

缺点:元素画画只能画点,没办法连线。


用canvas画画

  • 矩形

    获取画板 var yyy =document.getElementById(‘xxx’);

    获取2d上下文 var context =yyy.getContext(“2d”);

    填充样式 context.fillStyle = ‘red’;

    填充、描边矩形 context.fillRect(0,0,100,100);

    描边样式 context.strokeStyle= ‘yellow’;

    描述矩形 context.strokeRect(10,0,100,100);

    清除 context.clearRect(50,50,60,60);

  • 画线

    开始 context.beginPath();

    起点 context.moveTo(80,80);

    画线 context.lineTo(100,80);

    结束 context.fill();

  • 画圆

    开始 context.beginPath()

    画圆 context.arc(150,150,半径,开始角度,结束角度)

    结束 context.fill()

  • 点与线连起来

出现第1个点第2个点的时候之间连个线,即mouseDown的时候为第1个点,mouseMove的时候为第2个点。并每次记录上1个点。


获取页面宽高

document.documentElement.clientWidth

document.documentElement.clientHeight


一个按钮只做一件事


让页面缩放

content="width=device-width,minimum-scale=1.0,maximum-scale=1.0"/>


in操作符

varhash = {a:1,b:2,c:3}

‘a’in hash——》true

‘d’in hash——》false


禁止滚动条 overflow:hidden

在移动端让canvas不移动 绝对定位 position:fixed; top:0; left:0;

5.实际成果

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,748评论 1 45
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,054评论 0 2
  • 相关知识点 移动端、 适配(兼容)、 ios点击事件300ms延迟、 点击穿透、 定位失效...... 问题&解决...
    sandisen阅读 25,590评论 3 67
  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 7,842评论 2 17
  • 今天是艺萱放假第二天,我给她出的数学作业。她做完了叫我检查,一开始检查结果错两道题,叫她重做。过了一会,艺萱说:妈...
    陈艺萱妈妈阅读 133评论 0 2