此代码在PC浏览器上效果很好,但在移动设备上不起作用

WUSO01
<!doctype html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8" />  
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />  
    <title> canvas  </title>  
    <style type="text/css">  
        #canvasId {  
            background-color: #FFFFcc;  
        }  
    </style>  

</head>  
<body>  

    <canvas id="canvasId" width="700" height="500"></canvas><br />  
    <input type="button" value="clear" onclick="hw.clear();" /> 
    <script type="text/javascript">  
        function Handwriting(id) {  
            this.canvas = document.getElementById(id);  
            this.ctx = this.canvas.getContext("2d");  
            this.ctx.fillStyle = "rgba(0,0,0,0.25)";  
            var _this = this;  
            this.canvas.onmousedown = function (e) { _this.downEvent(e)};  
            this.canvas.onmousemove = function (e) { _this.moveEvent(e)};  
            this.canvas.onmouseup = function (e) { _this.upEvent(e)};  
            this.canvas.onmouseout = function (e) { _this.upEvent(e)}; 
            this.canvas.addEventListener('touchstart', function (e) { _this.downEvent(e)});  
            this.canvas.addEventListener('touchmove ', function (e) { _this.moveEvent(e)});  
            this.canvas.addEventListener('touchend ', function (e) { _this.upEvent(e)}); 
            this.moveFlag = false;  
            this.upof = {};  
            this.radius = 0;  
            this.has = [];  
            this.lineMax = 15;  
            this.lineMin = 1;  
            this.linePressure = 1;  
            this.smoothness = 80;  
        }            
        Handwriting.prototype.clear = function () {  
            this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);  
        }  
        Handwriting.prototype.downEvent = function (e) {  
            this.moveFlag = true;  
            this.has = [];  
            this.upof = this.getXY(e);  
        }            
        Handwriting.prototype.moveEvent = function (e) {  
            if (!this.moveFlag)  
                return;  
            var of = this.getXY(e);  
            var up = this.upof;  
            var ur = this.radius;  
            this.has.unshift({time:new Date().getTime() ,dis:this.distance(up,of)});  
            var dis = 0;  
            var time = 0;  
            for (var n = 0; n < this.has.length-1; n++) {  
                dis += this.has[n].dis;  
                time += this.has[n].time-this.has[n+1].time;  
                if (dis>this.smoothness)  
                    break;  
            }  
            var or = Math.min(time/dis*this.linePressure+this.lineMin , this.lineMax) / 2;  
            this.radius = or;  
            this.upof = of;  
            if (this.has.length<=4)  
                return;  
            var len = Math.round(this.has[0].dis/2)+1;  
            for (var i = 0; i < len; i++) {  
                var x = up.x + (of.x-up.x)/len*i;  
                var y = up.y + (of.y-up.y)/len*i;  
                var r = ur + (or-ur)/len*i;  
                this.ctx.beginPath();  
                this.ctx.arc(x,y,r,0,2*Math.PI,true);  
                this.ctx.fill();  
            }  
        }            
        Handwriting.prototype.upEvent = function (e) {  
            this.moveFlag = false;  
        }            
        Handwriting.prototype.getXY = function (e)  
        {  
            return {  
                x : e.clientX - this.canvas.offsetLeft + (document.body.scrollLeft || document.documentElement.scrollLeft),  
                y : e.clientY - this.canvas.offsetTop  + (document.body.scrollTop || document.documentElement.scrollTop)  
            }  
        }            
        Handwriting.prototype.distance = function (a,b)  
        {  
            var x = b.x-a.x , y = b.y-a.y;  
            return Math.sqrt(x*x+y*y);  
        }   
        var hw = new Handwriting("canvasId");  
        hw.lineMax = 40;  
        hw.lineMin = 5;  
        hw.linePressure = 2.5;  
        hw.smoothness = 100;    
    </script>  
</body>  
</html>

我的老板要我使用canvas并实现签名功能,但是我不擅长,所以我找到了这段代码,但是我不知道为什么它不起作用,我添加了代码,this.canvas.addEventListener('touchstart', function (e) { _this.downEvent(e)});但是它仍然不起作用,所以你们会发现出去?多谢!

杰罗曼达X

首先-'touchmove '而且'touchend '不应该有他们的空间

所以你需要修复

this.canvas.addEventListener('touchmove', function (e) { _this.moveEvent(e)});  
this.canvas.addEventListener('touchend', function (e) { _this.upEvent(e)}); 

然后getXY函数也需要getXY来进行触摸事件

Handwriting.prototype.getXY = function(e) {
    var x, y;
    if (e.changedTouches && e.changedTouches[0]) {
        var offsety = this.canvas.offsetTop || 0;
        var offsetx = this.canvas.offsetLeft || 0;
        x = e.changedTouches[0].pageX - offsetx;
        y = e.changedTouches[0].pageY - offsety;
    } else if (e.layerX || 0 == e.layerX) {
        x = e.layerX;
        y = e.layerY;
    } else if (e.offsetX || 0 == e.offsetX) {
        x = e.offsetX;
        y = e.offsetY;
    }
    return {
        x: x,
        y: y
    };
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

SAPUI5 keyup 事件在移动设备的浏览器上不起作用

来自分类Dev

为什么此Jquery代码在IE上不起作用?(在其他浏览器上完美运行)

来自分类Dev

为什么此create-react-app在移动浏览器上不起作用?

来自分类Dev

ui-sref在ionicview上不起作用,但在浏览器,设备和模拟器上起作用

来自分类Dev

AngularJS在某些浏览器/设备上不起作用

来自分类Dev

AngularJS在某些浏览器/设备上不起作用

来自分类Dev

宽度:100%在移动浏览器上不起作用

来自分类Dev

为什么我的jquery代码在移动浏览器上不起作用

来自分类Dev

按钮在移动设备上不起作用,但在PC引导程序上起作用

来自分类Dev

JPlayer在Firefox Android上不起作用,但在所有其他浏览器上都起作用

来自分类Dev

为什么我的代码在设备上不起作用,但在模拟器上起作用?

来自分类Dev

为什么地理定位在移动浏览器上不起作用?

来自分类Dev

Selectize.js-退格键以清除该字段在移动浏览器上不起作用

来自分类Dev

Ajax功能在移动浏览器上不起作用

来自分类Dev

选择的插件似乎在移动浏览器上不起作用

来自分类Dev

Flex证明内容,对齐项在Safari或移动浏览器上不起作用

来自分类Dev

Facebook App链接(深层链接)在移动浏览器上不起作用

来自分类Dev

为什么(MouseUp mouseDown)在移动浏览器上不起作用?

来自分类Dev

@media max-width在移动浏览器上不起作用?

来自分类Dev

为什么 fetch 在我的移动浏览器上不起作用?

来自分类Dev

MKLocalSearchCompleter在物理设备上不起作用,但在模拟器上可以正常工作

来自分类Dev

HTML Dropdown的重定向(使用JS)在移动设备上不起作用,但在PC上工作正常

来自分类Dev

Ajax 代码在移动设备上不起作用

来自分类Dev

getUserMedia在新的浏览器上不起作用

来自分类Dev

setOnMouseDragged在浏览器视图上不起作用

来自分类Dev

JavaScript pushState在返回的浏览器上不起作用

来自分类Dev

字体在Windows版本的浏览器上不起作用

来自分类Dev

Atrributes Selection在Android浏览器上不起作用

来自分类Dev

jQuery POST在某些浏览器上不起作用

Related 相关文章

  1. 1

    SAPUI5 keyup 事件在移动设备的浏览器上不起作用

  2. 2

    为什么此Jquery代码在IE上不起作用?(在其他浏览器上完美运行)

  3. 3

    为什么此create-react-app在移动浏览器上不起作用?

  4. 4

    ui-sref在ionicview上不起作用,但在浏览器,设备和模拟器上起作用

  5. 5

    AngularJS在某些浏览器/设备上不起作用

  6. 6

    AngularJS在某些浏览器/设备上不起作用

  7. 7

    宽度:100%在移动浏览器上不起作用

  8. 8

    为什么我的jquery代码在移动浏览器上不起作用

  9. 9

    按钮在移动设备上不起作用,但在PC引导程序上起作用

  10. 10

    JPlayer在Firefox Android上不起作用,但在所有其他浏览器上都起作用

  11. 11

    为什么我的代码在设备上不起作用,但在模拟器上起作用?

  12. 12

    为什么地理定位在移动浏览器上不起作用?

  13. 13

    Selectize.js-退格键以清除该字段在移动浏览器上不起作用

  14. 14

    Ajax功能在移动浏览器上不起作用

  15. 15

    选择的插件似乎在移动浏览器上不起作用

  16. 16

    Flex证明内容,对齐项在Safari或移动浏览器上不起作用

  17. 17

    Facebook App链接(深层链接)在移动浏览器上不起作用

  18. 18

    为什么(MouseUp mouseDown)在移动浏览器上不起作用?

  19. 19

    @media max-width在移动浏览器上不起作用?

  20. 20

    为什么 fetch 在我的移动浏览器上不起作用?

  21. 21

    MKLocalSearchCompleter在物理设备上不起作用,但在模拟器上可以正常工作

  22. 22

    HTML Dropdown的重定向(使用JS)在移动设备上不起作用,但在PC上工作正常

  23. 23

    Ajax 代码在移动设备上不起作用

  24. 24

    getUserMedia在新的浏览器上不起作用

  25. 25

    setOnMouseDragged在浏览器视图上不起作用

  26. 26

    JavaScript pushState在返回的浏览器上不起作用

  27. 27

    字体在Windows版本的浏览器上不起作用

  28. 28

    Atrributes Selection在Android浏览器上不起作用

  29. 29

    jQuery POST在某些浏览器上不起作用

热门标签

归档