博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
html5 图片拖动效果,HTML5 P5.js 图片拖动交互效果
阅读量:6911 次
发布时间:2019-06-27

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

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

"use strict";

/*

Johan Karlsson (DonKarlssonSan)

Dragging images

*/

var Rectangle = (function() {

function Rectangle(pos, img) {

this.pos = pos;

this.img = img;

this.width = img.width;

this.height = img.height;

}

Rectangle.prototype.draw = function() {

image(this.img, this.pos.x, this.pos.y);

};

Rectangle.prototype.hits = function(hitpos) {

if (hitpos.x > this.pos.x &&

hitpos.x < this.pos.x + this.width &&

hitpos.y > this.pos.y &&

hitpos.y < this.pos.y + this.height) {

return true;

}

return false;

};

return Rectangle;

}());

var rects;

var dragRec;

var isDragging;

var clickOffset;

var imgCb;

function preload() {

imgCb = loadImage("/assets/coolgirl.jpg");

}

function setup() {

rects = [];

placeImages();

isDragging = false;

createCanvas(windowWidth, windowHeight);

}

function placeImages() {

var numImage = 5;

for (var i = 0; i < numImage; i++) {

var pos = randomPos();

rects.push(new Rectangle(pos, imgCb));

}

}

function randomPos() {

return createVector(random(0, windowWidth), random(0, windowHeight));

}

function draw() {

clear();

rects.forEach(function(r) {

return r.draw();

});

}

function mousePressed() {

var m = createVector(mouseX, mouseY);

var index;

rects.forEach(function(r, i) {

if (r.hits(m)) {

clickOffset = p5.Vector.sub(r.pos, m);

isDragging = true;

dragRec = r;

index = i;

}

});

if (isDragging) {

putOnTop(index);

}

}

function putOnTop(index) {

rects.splice(index, 1);

rects.push(dragRec);

}

function mouseDragged() {

if (isDragging) {

var m = createVector(mouseX, mouseY);

dragRec.pos.set(m).add(clickOffset);

}

}

function mouseReleased() {

isDragging = false;

}

function windowResized() {

resizeCanvas(windowWidth, windowHeight);

}

转载地址:http://mincl.baihongyu.com/

你可能感兴趣的文章
一次编程中无意碰见的问题
查看>>
docker常用命令
查看>>
POJ 1979 红与黑
查看>>
Excel基础知识(一)
查看>>
网络编程 ------ 基础
查看>>
10.31T3 其他算法思想
查看>>
Java 线程 — ScheduledThreadPoolExecutor
查看>>
Microsoft Dynamics CRM 常用JS语法(已转成vs2017语法提示)
查看>>
map()和filter()和reduce()函数
查看>>
python从入门到实践-8章函数
查看>>
3.MyBatis常用
查看>>
WEB学习
查看>>
Algorithm | Sort
查看>>
TF-IDF与余弦相似性的应用(一):自动提取关键词
查看>>
web2py官方文档翻译01
查看>>
《go语言程序设计》学习(三)
查看>>
.NET Framework 4.5 五个新特性
查看>>
从后台转前端的一些感想
查看>>
bulid runnable jar file with dependencies and main class
查看>>
vsftpd conf 解釋
查看>>