<template>
<div class="hello">
<!--touchstart,touchmove,touchend,touchcancel 这-->
<button type="" v-on:click="clear">清除</button>
<button v-on:click="save">保存</button>
<canvas id="canvas" width="" height="" style="border:1px solid black">Canvas画板</canvas>
<img v-bind:src="url" alt="">
</div> </template> <script>
var draw;
var preHandler = function(e){e.preventDefault();}
class Draw {
constructor(el) {
this.el = el
this.canvas = document.getElementById(this.el)
this.cxt = this.canvas.getContext('2d')
this.stage_info = canvas.getBoundingClientRect()
this.path = {
beginX: ,
beginY: ,
endX: ,
endY:
}
}
init(btn) {
var that = this; this.canvas.addEventListener('touchstart', function(event) {
document.addEventListener('touchstart', preHandler, false);
that.drawBegin(event)
})
this.canvas.addEventListener('touchend', function(event) {
document.addEventListener('touchend', preHandler, false);
that.drawEnd() })
this.clear(btn)
}
drawBegin(e) {
var that = this;
window.getSelection() ? window.getSelection().removeAllRanges() : document.selection.empty()
this.cxt.strokeStyle = "#000"
this.cxt.beginPath()
this.cxt.moveTo(
e.changedTouches[].clientX - this.stage_info.left,
e.changedTouches[].clientY - this.stage_info.top
)
this.path.beginX = e.changedTouches[].clientX - this.stage_info.left
this.path.beginY = e.changedTouches[].clientY - this.stage_info.top
canvas.addEventListener("touchmove",function(){
that.drawing(event)
})
}
drawing(e) {
this.cxt.lineTo(
e.changedTouches[].clientX - this.stage_info.left,
e.changedTouches[].clientY - this.stage_info.top
)
this.path.endX = e.changedTouches[].clientX - this.stage_info.left
this.path.endY = e.changedTouches[].clientY - this.stage_info.top
this.cxt.stroke()
}
drawEnd() {
document.removeEventListener('touchstart', preHandler, false);
document.removeEventListener('touchend', preHandler, false);
document.removeEventListener('touchmove', preHandler, false);
//canvas.ontouchmove = canvas.ontouchend = null
}
clear(btn) {
this.cxt.clearRect(, , , )
}
save(){
return canvas.toDataURL("image/png")
}
} export default {
data () {
return {
msg: 'Welcome to Your Vue.js App',
val:true,
url:""
}
},
mounted() {
draw=new Draw('canvas');
draw.init();
},
methods:{
clear:function(){
draw.clear();
},
save:function(){
var data=draw.save();
this.url = data;
console.log(data)
},
   mutate(word) {
this.$emit("input", word);
},
} } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> h1, h2 { font-weight: normal; } ul { list-style-type: none; padding: ; } li { display: inline-block; margin:  10px; } a { color: #42b983; } #canvas { background: pink; cursor: default; } #keyword-box { margin: 10px ; } </style>

Vue利用canvas实现移动端手写板的更多相关文章

  1. 利用canvas压缩图片

    现在手机拍的照片动不动就是几M,当用户上传手机里的照片时一个消耗流量大,一个上传时间长,为了解决这个问题,就需要压缩图片: 想法:利用canvas重绘图片,保持宽高比不变,具体宽高根本具体情况而定. ...

  2. HTML5利用canvas,把多张图合并成一张图片

    需求分析,根据当前网页中的几张图片,在手机上长按,保存图片到相册或者发送给好友. drawCanvas(){ var self = this; var imgsrcArray = [ require( ...

  3. 利用canvas对上传图片进行上传前压缩

    利用谷歌调式工具发现,图片大小直接影响着首屏加载时间. 且考虑到后期服务端压力,图片压缩特别必要. 本文是前端利用canvas实现图片.参考文章:https://www.cnblogs.com/007 ...

  4. 使用 Vue 2.0 实现服务端渲染的 HackerNews

    Vue 2.0 支持服务端渲染 (SSR),并且是流式的,可以做组件级的缓存,这使得极速渲染成为可能.同时, 和 2.0 也都能够配合 SSR 提供同构路由和客户端 state hydration.v ...

  5. 利用 canvas 破解 某拖动验证码

    利用 canvas 破解 某拖动验证码 http://my.oschina.net/u/237940/blog/337194

  6. 利用Canvas进行绘制XY坐标系

    首先来一发图 绘制XY的坐标主要是利用Canvas setLeft和setBottom功能(Canvas内置坐标的功能) 1.首先WPF中的坐标系都是从左到右,从上到下的 即左上角位置(0,0)点,所 ...

  7. 利用canvas实现的中点Bresenham算法

    Bresenham提出的直线生成算法的基本原理是,每次在最大位移方向上走一步,而另一个方向是走步还是不走步取决于误差项的判别,具体的实现过程大家可以去问度娘.我主要是利用canvas画布技术实现了这个 ...

  8. 小程序 青少儿书画 利用engineercms作为服务端

    因为很多妈咪们喜欢发布自己宝宝的作品,享受哪些美好时刻,记录亲子创作过程. 为了方便妈咪们展示亲子创作,比如宝宝们画作,涂鸦,书法,作文,其他才艺,特利用engineercms作为服务端,重新设计了一 ...

  9. html5 canvas 画图移动端出现锯齿毛边的解决方法

    使用HTML5的canvas元素画出来的.在移动端手机上测试都发现画图有一点锯齿问题 出现这个问题的原因应该是手机的宽是720像素的, 而这个canvas是按照小于720像素画出来的, 所以在720像 ...

随机推荐

  1. WEB安全系列之如何挖掘任意用户登录漏洞

    WEB安全系列之如何挖掘任意用户登录漏洞 0x01  前言        每周两篇文章打卡.坏蛋100块钱都不给我,好坏好坏的. 0x02  什么是任意用户登录漏洞   几乎每个网站都有自己的会员系统 ...

  2. [Swift]LeetCode143. 重排链表 | Reorder List

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...

  3. [Swift]LeetCode669. 修剪二叉搜索树 | Trim a Binary Search Tree

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...

  4. [Swift]LeetCode699. 掉落的方块 | Falling Squares

    On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...

  5. Spring Cloud Greenwich 正式发布,Hystrix 即将寿终正寝。。

    Spring Cloud Greenwich 正式版在 01/23/2019 这天正式发布了,下面我们来看下有哪些更新内容. 生命周期终止提醒 Spring Cloud Edgware Edgware ...

  6. JDK 8u131

    JDK 8u131 发布了.Java SE 8u131 包括重要的安全修复和bug修复.Oracle 强烈建议所有 JavaSE 8 用户升级到此版本.此次完整版本号为1.8.0_131-b11. J ...

  7. Python内置函数(36)——iter

    英文文档: iter(object[, sentinel]) Return an iterator object. The first argument is interpreted very dif ...

  8. spark在idea中本地如何运行?(处理问题NoSuchFieldException: SHUTDOWN_HOOK_PRIORITY)

    spark在idea中本地如何运行? 前几天尝试使用idea在本地运行spark+scala的程序,出现了问题,http://www.cnblogs.com/yjf512/p/7662105.html ...

  9. HttpClientFactory与Steeltoe结合来完成服务发现

    前言 上一篇说了一下用HttpClientFactory实现了简单的熔断降级. 这篇就来简单说说用HttpClientFactory来实现服务发现.由于标题已经好明显的说了Steeltoe 因此这里会 ...

  10. I-think-2

    最近自己的压力的确很大,对自己近期思想情况做一个总结. 本来假期都自己的未来已经做了一个详细的规划:其中近期的目标就是考上一所自己心仪的大学去读研究生,并且自己也选好了大学,作为自己研究生的目标--- ...