<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. QEMU KVM Libvirt手册(9): network

    虚拟网卡由-net nic定义 # qemu-system-x86_64 -enable-kvm -name ubuntutest  -m 2048 -hda ubuntutest.img -vnc ...

  2. Dubbo架构设计及原理详解

    Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来看,Dubbo采用的是一种非常简单的模 ...

  3. js查重去重性能优化心得

    概述 今天产品反映有个5000条数据的页面的保存按钮很慢,查看代码看到是因为点击保存按钮之后,进行了查重操作,而查重操作是用2个for循环完成了,时间复杂度是O(n^2).没办法,只能想办法优化一下了 ...

  4. [Swift]LeetCode81. 搜索旋转排序数组 II | Search in Rotated Sorted Array II

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  5. [Swift]LeetCode724. 寻找数组的中心索引 | Find Pivot Index

    Given an array of integers nums, write a method that returns the "pivot" index of this arr ...

  6. [Swift]LeetCode779. 第K个语法符号 | K-th Symbol in Grammar

    On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace ...

  7. SSM框架报HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException错

    如下图 一番排查之后发现原来是server层写漏注释了 粗心大意,一天内出现两次写漏注释,SSM框架有意思.

  8. C# listview展示表格格式

    有时候我们需要展示表格格式的数据,首先想到的是用datagridview控件,比如更改datagridview某一行的数据,这样操作起来就比较麻烦,而listview属于轻量级,刷新和更改相对来说效率 ...

  9. 客户端通过Feign发起请求 服务端通过request取 json对象

    @RestController @RequestMapping(value = "test") public class TestServer { @RequestMapping( ...

  10. Spark中SQL列和并为一行

    在使用数据库的时候,需要将查询出来的一列按照逗号合并成一行. 原表名字为 TABLE ,表中的部分原始数据为: +---------+------------------------+ | BASIC ...