html5 svg动画示范,HTML5 SVG 经典链式反应动画演示

这段代码展示了一个使用JavaScript、Babel和CoffeeScript实现的交互式游戏,玩家可以点击旋转方块,方块之间通过特定规则进行旋转交互。游戏的核心是 Dot 类,它包含了方块的旋转、碰撞检测和点击响应等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

(function() {

var $chart, $main, $row, $score, Dot, Ping, Points, Stop, Tick, dot, dots, getDot, i, j, rand, svgNs, x, xlinkNs, y;

svgNs = "http://www.w3.org/2000/svg";

xlinkNs = "http://www.w3.org/1999/xlink";

$score = document.querySelector("#score");

$main = document.querySelector("main");

$chart = document.querySelector("#chart");

rand = function(min, max) {

return min + Math.floor(Math.random() * (max - min));

};

dots = [];

getDot = function(x, y) {

if (y < 0) {

return null;

}

if (y >= dots.length) {

return null;

}

if (x < 0) {

return null;

}

if (x >= dots[y].length) {

return null;

}

return dots[y][x];

};

Points = 0;

Dot = function($wrapper, x, y) {

var $column, $use, pingMaybe, refresh, self;

self = this;

$column = document.createElementNS(svgNs, "svg");

$column.setAttributeNS(svgNs, "viewBox", "0 0 20 20");

$column.setAttribute("data-x", x);

$column.style.top = (y * 22) + "px";

$column.style.left = (x * 22) + "px";

$use = document.createElementNS(svgNs, "use");

$use.setAttributeNS(xlinkNs, "xlink:href", "#dot");

$column.appendChild($use);

$wrapper.appendChild($column);

refresh = function(plus) {

if (plus == null) {

plus = 0;

}

self.rotation += plus;

return $column.style.transform = "rotate(" + (self.rotation * 90) + "deg)";

};

self.getRotation = function() {

return ((self.rotation % 4) + 4) % 4;

};

self.stop = function() {

self.pleaseRotate = false;

return self.pleasePing = false;

};

self.stop();

self.randomize = function() {

self.rotation = rand(0, 3);

return refresh();

};

self.randomize();

/*

pingDirection

↑ - 0

→ - 1

↓ - 2

← - 3

*/

pingMaybe = function(pingDirection) {

var canPing, pingX, pingY, victim;

pingX = x;

pingY = y;

if (pingDirection === 0) {

pingY -= 1;

}

if (pingDirection === 1) {

pingX += 1;

}

if (pingDirection === 2) {

pingY += 1;

}

if (pingDirection === 3) {

pingX -= 1;

}

victim = getDot(pingX, pingY);

if (victim !== null) {

canPing = false;

if ((pingDirection === 1) || (pingDirection === 2)) {

if (victim.getRotation() === 0) {

canPing = true;

}

}

if ((pingDirection === 2) || (pingDirection === 3)) {

if (victim.getRotation() === 1) {

canPing = true;

}

}

if ((pingDirection === 3) || (pingDirection === 0)) {

if (victim.getRotation() === 2) {

canPing = true;

}

}

if ((pingDirection === 0) || (pingDirection === 1)) {

if (victim.getRotation() === 3) {

canPing = true;

}

}

if (canPing) {

return victim.pleaseRotate = true;

}

}

};

/*

pingRotation

┛ - 0

┗ - 1

┏ - 2

┓ - 3

*/

self.ping = function() {

var pingRotation;

if (!self.pleasePing) {

return;

}

pingRotation = self.getRotation();

if (pingRotation === 0) {

pingMaybe(0);

pingMaybe(3);

}

if (pingRotation === 1) {

pingMaybe(0);

pingMaybe(1);

}

if (pingRotation === 2) {

pingMaybe(1);

pingMaybe(2);

}

if (pingRotation === 3) {

pingMaybe(2);

pingMaybe(3);

}

return self.pleasePing = false;

};

$column.addEventListener("click", function(event) {

Stop();

self.pleaseRotate = true;

return refresh();

});

self.update = function() {

if (self.pleaseRotate) {

refresh(1);

self.pleaseRotate = false;

self.pleasePing = true;

return 1;

}

return 0;

};

return self;

};

$main.style.width = (40 * 22) + "px";

$main.style.height = (20 * 22) + "px";

for (y = i = 0; i < 20; y = ++i) {

$row = document.createElement("div");

$row.setAttribute("data-y", y);

dots[y] = [];

for (x = j = 0; j < 40; x = ++j) {

dot = new Dot($row, x, y);

dots[y][x] = dot;

}

$main.appendChild($row);

}

Stop = function() {

var k, len, results, row;

Points = 0;

$score.textContent = Points;

$score.className = "";

$chart.innerHTML = "";

results = [];

for (k = 0, len = dots.length; k < len; k++) {

row = dots[k];

results.push((function() {

var l, len1, results1;

results1 = [];

for (l = 0, len1 = row.length; l < len1; l++) {

dot = row[l];

results1.push(dot.stop());

}

return results1;

})());

}

return results;

};

this.Randomize = function() {

var k, l, len, len1, row;

Stop();

for (k = 0, len = dots.length; k < len; k++) {

row = dots[k];

for (l = 0, len1 = row.length; l < len1; l++) {

dot = row[l];

dot.randomize();

}

}

return false;

};

Ping = function() {

var k, len, results, row;

results = [];

for (k = 0, len = dots.length; k < len; k++) {

row = dots[k];

results.push((function() {

var l, len1, results1;

results1 = [];

for (l = 0, len1 = row.length; l < len1; l++) {

dot = row[l];

results1.push(dot.ping());

}

return results1;

})());

}

return results;

};

Tick = function() {

var $bar, k, l, len, len1, row, tickPoints;

tickPoints = 0;

for (k = 0, len = dots.length; k < len; k++) {

row = dots[k];

for (l = 0, len1 = row.length; l < len1; l++) {

dot = row[l];

tickPoints += dot.update();

}

}

if (tickPoints > 0) {

$bar = document.createElement("b");

$bar.style.height = tickPoints + "px";

$chart.appendChild($bar);

}

if (tickPoints === 0) {

$score.className = "finished";

}

Points += tickPoints;

$score.textContent = Points;

return window.setTimeout(Ping, 0);

};

window.setInterval(Tick, 500);

}).call(this);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值