博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
touch event
阅读量:5018 次
发布时间:2019-06-12

本文共 2980 字,大约阅读时间需要 9 分钟。

/** * Author: humanhuang * Date: 13-12-12 */var canvas = document.getElementById("canvas"),    stacks = [{        color: "red"    }, {        color: "blue"    }, {        color: "green"    }, {        color: "black"    }, {        color: "yellow"    }, {        color: "orange"    }];function getEmptyStack() {    var i = 0, s, l = stacks.length;    for (; i < l; i++) {        s = stacks[i];        if (!s.touchId)            return s;    }    return null;}function getStackByTouchId(touchId) {    var i = 0, s, l = stacks.length;    for (; i < l; i++) {        s = stacks[i];        if (s.touchId == touchId)            return s;    }    return null;}function touchStart(e) {    e.preventDefault();    var touches = e.changedTouches,        i = 0, l = touches.length, touch, stack;    for (; i < l; i++) {        touch = touches[i];        stack = getStackByTouchId(touch.identifier);        if (stack) return;        stack = getEmptyStack();        if (stack) {            stack.touchId = touch.identifier;            stack.spirit = document.createElement("div");            stack.spirit.className = "spirit";            stack.startX = touch.pageX;            stack.startY = touch.pageY;            stack.spirit.style.left = touch.pageX + "px";            stack.spirit.style.top = touch.pageY + "px";            stack.spirit.style.backgroundColor = stack.color;            canvas.appendChild(stack.spirit);        } else { // stack list is full            return;        }    }}function touchMove(e) {    e.preventDefault();    var touches = e.targetTouches,        i = 0, l = touches.length, touch, stack;    for (; i < l; i++) {        touch = touches[i];        stack = getStackByTouchId(touch.identifier);        if (stack) {            var x = touch.pageX - stack.startX,                y = touch.pageY - stack.startY,                spirit = stack.spirit;            spirit.style.webkitTransform = 'translate(' + x + 'px, ' + y + 'px)';        }    }}function touchEnd(e) {    var touches = e.changedTouches,        i = 0, l = touches.length, touch, stack;//        alert(e.targetTouches.length);    for (;i < l; i++) {        touch = touches[i];        stack = getStackByTouchId(touch.identifier);        if (stack) {            stack.touchId = null;            canvas.removeChild(stack.spirit);            stack.spirit = null;        }    }}function touchCancel(e) {    var touches = e.changedTouches,        i = 0, l = touches.length, touch, stack;    for (;i < l; i++) {        touch = touches[i];        stack = getStackByTouchId(touch.identifier);        if (stack) {            stack.touchId = null;            canvas.removeChild(stack.spirit);            stack.spirit = null;        }    }}canvas.addEventListener("touchstart", touchStart, false);canvas.addEventListener("touchmove", touchMove, false);canvas.addEventListener("touchend", touchEnd, false);canvas.addEventListener("touchCancel", touchCancel, false);

 

转载于:https://www.cnblogs.com/human/p/3471910.html

你可能感兴趣的文章
MySQL性能测试工具之mysqlslap使用详解
查看>>
深入理解jsonp跨域请求原理
查看>>
regsvr32注册COM组件失败
查看>>
jmeter,CSV数据加载、数据库连接、正则
查看>>
MySQL学习点滴 --分区表
查看>>
4.6.1 测试基础
查看>>
洛谷 P2486 [SDOI2011]染色
查看>>
oo第三单元总结
查看>>
leetcode : Count and Say [基本功]
查看>>
洛谷 P2485 [SDOI2011]计算器 解题报告
查看>>
Slickflow.NET 开源工作流引擎基础介绍(三) -- 基于HTML5/Bootstrap的Web流程设计器
查看>>
Node教程
查看>>
java将字段映射成另一个字段,关于 接口传参 字段不对应转换
查看>>
Redis
查看>>
HTTP(一)工作机制
查看>>
条形码扫描枪数据读取的问题
查看>>
健壮的 Java 基准测试
查看>>
phpstorm查看类的继承关系
查看>>
Amd,Cmd, Commonjs, ES6 import/export的异同点
查看>>
Ubuntu 18.04安装arm-linux-gcc交叉编译器
查看>>