web涂鸦展示-canvas的线应用的demo

一、参数来源于Android客户端
二、html页面
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title> title </title>
        <script src="jquery.js"></script>
        <style type="text/css">
            canvas{
                border: 1px solid black;
            }
        </style>
    </head>
    <body onload="init();">
    this is body:<br/>
    <canvas id="test-canvas" width="1340px" height="500px">抱歉,你的浏览器不支持canvas(当浏览器不支持时,canvas标签内的内容会被显示)</canvas>
    </body>
</html>
三、主要js控制

注:(此处所用demo的info虽然长度就2,但数据结构比较复杂,扩展性比较好,可以正反撤销,只显示某个人的等复杂的涂鸦操作。info正常情况下是画一笔传一次,对lines追加进行追加)

<script type="text/javascript">
    var info = '[{"author":"88A0864BBEE246BF8ED59EA3FEE26E40","color":1,"id":1,"points":[{"x":0.74375,"y":0.12039801},{"x":0.7476631,"y":0.111881405},{"x":0.7859946,"y":0.087521285},{"x":0.82673794,"y":0.08850872},{"x":0.84499437,"y":0.09974015},{"x":0.8853753,"y":0.17132203},{"x":0.8897556,"y":0.21296544},{"x":0.8675483,"y":0.30277675},{"x":0.8380556,"y":0.351431},{"x":0.76671785,"y":0.4415483},{"x":0.722152,"y":0.49456975},{"x":0.7213542,"y":0.49552238}],"width":0.0020833334},{"author":"88A0864BBEE246BF8ED59EA3FEE26E40","color":1,"id":2,"points":[{"x":0.69375,"y":0.55422884},{"x":0.7009463,"y":0.54679185},{"x":0.71752954,"y":0.544742},{"x":0.72783816,"y":0.5712108},{"x":0.72734267,"y":0.6173053},{"x":0.71250314,"y":0.70410824},{"x":0.7048618,"y":0.73096216},{"x":0.7007172,"y":0.7429573},{"x":0.6974098,"y":0.74925375},{"x":0.69799846,"y":0.74352187},{"x":0.7047093,"y":0.71900743},{"x":0.7151771,"y":0.6937196},{"x":0.7264439,"y":0.67380184},{"x":0.73568565,"y":0.66138315},{"x":0.7408195,"y":0.6571788},{"x":0.7433488,"y":0.65734464},{"x":0.7453125,"y":0.67542917},{"x":0.7435694,"y":0.7035472},{"x":0.7385216,"y":0.7283287},{"x":0.72257334,"y":0.75484276},{"x":0.71770835,"y":0.74370515},{"x":0.72069967,"y":0.7128979},{"x":0.7294462,"y":0.6836851},{"x":0.7451303,"y":0.65378714},{"x":0.7606162,"y":0.62946516},{"x":0.77008843,"y":0.61740845},{"x":0.77635115,"y":0.6105905},{"x":0.7793638,"y":0.607503},{"x":0.7758453,"y":0.57187814},{"x":0.7839416,"y":0.5072019},{"x":0.8098504,"y":0.47055495},{"x":0.79448074,"y":0.55937546},{"x":0.7610427,"y":0.66774344},{"x":0.7557772,"y":0.6846293},{"x":0.75351787,"y":0.6926511},{"x":0.75951856,"y":0.6945274},{"x":0.7791436,"y":0.6695561},{"x":0.80038416,"y":0.63468504},{"x":0.8199764,"y":0.58985746},{"x":0.82938135,"y":0.55807644},{"x":0.8322831,"y":0.54168004},{"x":0.8328125,"y":0.53454083},{"x":0.81875,"y":0.5393035},{"x":0.8007047,"y":0.5842821},{"x":0.78573895,"y":0.63312054},{"x":0.7790026,"y":0.6745721},{"x":0.7776506,"y":0.6875685},{"x":0.7765945,"y":0.6962112},{"x":0.77891,"y":0.66761684},{"x":0.78773016,"y":0.5853061},{"x":0.790335,"y":0.57161635},{"x":0.7942515,"y":0.56319636},{"x":0.8126348,"y":0.6456677},{"x":0.82694346,"y":0.7595096},{"x":0.828125,"y":0.76915425},{"x":0.828125,"y":0.76915425}],"width":0.0020833334}]';
    var lines;
    var canvasWidth = 1340;
    var canvasHeight = 500;
    var drawLines = function(lineId){
        var canvas = document.getElementById('test-canvas');
        if(canvas==null){
            return;
        }
        var ctx = document.getElementById('test-canvas').getContext('2d');
        var linesLenth = lines.length;
        console.log("linesLenth:"+linesLenth);
        for(var i = 0;i < linesLenth; i++){
            var line = lines[i];
            if(lineId!=i){
                var lineColor;
                var lineWidth;
                var lineJoin = ['round','bevel','miter'];
                ctx.lineJoin = lineJoin[0];//属性值决定了图形中两线段连接处所显示的样子
                var lineCap = ['butt','round','square'];
                ctx.lineCap = lineCap[1];//属性 lineCap 的值决定了线段端点显示的样子
                ctx.lineWidth = canvasWidth*line.width;
                switch (line.color){
                    case 1: lineColor = "#000000";break;
                    case 2: lineColor = "#FF2A00";break;
                    case 3: lineColor = "#FFC600";break;
                    case 4: lineColor = "#00C6FF";break;
                    default: lineColor = "#000000";
                }
                ctx.strokeStyle = lineColor;
                var points = line.points;
                ctx.beginPath();
                var lastX = points[0].x;
                var lastY = points[0].y;
                ctx.moveTo(lastX*canvasWidth,lastY*canvasHeight);
                for(var j=0;j < points.length;j++){
                    var newX = points[j].x;
                    var newY = points[j].y;
                    ctx.quadraticCurveTo(lastX*canvasWidth,lastY*canvasHeight,((lastX+newX)/2)*canvasWidth,((lastY+newY)/2)*canvasHeight);
                    lastX = points[j].x;
                    lastY = points[j].y;
                }
                ctx.lineTo(lastX*canvasWidth,lastY*canvasHeight);
                ctx.stroke();
            }else{
                return;
            }
        }
    }
    var init = function(){
        lines = JSON.parse(info);
    }
    </script>
四、扩展js

追加lines

var saveDrawLine = function(line){
        var newLines = new Array(line.id);
        for(var i = 0;i < newLines.length;i ++){
            if(i!=newLines.length-1){
                newLines[i] = lines[i];
            }else{
                newLines[i] = line;
            }
        }
        lines = newLines;
        cleanCanvas();
        drawLines();
        return lines;
    }

清除画布重新绘制到哪个线

var reViewCanvas = function(info){
        var id = info;
        cleanCanvas();
        drawLines(id);
    }
var cleanCanvas = function clearCanvas(){
        var canvas = document.getElementById("test-canvas");
        if(canvas!=null){
            var cxt = canvas.getContext("2d");
            cxt.clearRect(0,0,canvas.width,canvas.height);
        }
    }

补充真的涂鸦其实还有参照的,比图在图片上涂鸦,所以真的在做涂鸦的功能的时候肯定是多个平台组合,此处先写到这里,也算符合标题了。

五、demo效果

浏览器控制台输入

drawLines(2)
demo效果图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,884评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,755评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,369评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,799评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,910评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,096评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,159评论 3 411
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,917评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,360评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,673评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,814评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,509评论 4 334
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,156评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,882评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,123评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,641评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,728评论 2 351

推荐阅读更多精彩内容