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

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

<!doctype html>

<html>
<head>
<meta charset="UTF-8" />
<title>Canvas Drag and Drop Test</title>
</head>
<body>
<section>

<div>

<canvas id="canvas" width="400" height="300">
This text is displayed if your browser does not support HTML5 Canvas.
</canvas>
</div>

<script type="text/javascript">

var canvas;

var ctx;
var x = 75;
var y = 50;
var WIDTH = 400;
var HEIGHT = 300;
var dragok = false;

function rect(x,y,w,h) {

ctx.beginPath();
ctx.rect(x,y,w,h);
ctx.closePath();
ctx.fill();
}

function clear() {

ctx.clearRect(0, 0, WIDTH, HEIGHT);
}

function init() {

canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
return setInterval(draw, 10);
}

function draw() {

clear();
ctx.fillStyle = "#FAF7F8";
rect(0,0,WIDTH,HEIGHT);
ctx.fillStyle = "#444444";
rect(x - 15, y - 15, 30, 30);
}

function myMove(e){

if (dragok){
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
}
}

function myDown(e){

if (e.pageX < x + 15 + canvas.offsetLeft && e.pageX > x - 15 +
canvas.offsetLeft && e.pageY < y + 15 + canvas.offsetTop &&
e.pageY > y -15 + canvas.offsetTop){
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
dragok = true;
canvas.onmousemove = myMove;
}
}

function myUp(){

dragok = false;
canvas.onmousemove = null;
}

init();

canvas.onmousedown = myDown;
canvas.onmouseup = myUp;

</script>

</section>

</body>
</html>

转载于:https://www.cnblogs.com/oxspirt/p/8945627.html

你可能感兴趣的文章
测试环境搭建及维护
查看>>
Django——12 中间件 上下文处理器 admin后台
查看>>
SQL Server查询性能优化——创建索引原则(一)
查看>>
spring 配置多数据源 (案例)
查看>>
ACM_回文素数
查看>>
微信程序开发
查看>>
搜索6--noi1700:八皇后问题
查看>>
Python基础第四课
查看>>
Spring Boot Maven 打包可执行Jar文件!
查看>>
js变量以及其作用域详解2
查看>>
符串的排列
查看>>
Java se 要点
查看>>
POJ 3260 多重背包+完全背包
查看>>
MySQL导出数据库、数据库表结构、存储过程及函数【用】
查看>>
JAVA 调用存储过程
查看>>
湖南卫视直播
查看>>
QPS,TPS,吞吐量,响应时间详解及关系
查看>>
SQL Server 事务隔离级别
查看>>
多线程学习 - 简单的购票程序
查看>>
2.2.1 PREFACE NUMBERING 序言页码
查看>>