- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近开始在一个网站上工作并学习 HTML/CCS/JS,并发现我不确定如何执行。基本上,我希望当前四处移动并从窗口边框反弹的 float 文本也相互反弹。我还认为数组可能会很好地能够设置我想要生成的文本对象的数量。这是我的网站,供引用文本如何弹跳 http://gmtrash.ga/
这里是控制文本对象和颜色的 JavaScript
setInterval(function() {
myContainer = document.getElementById("colortext");
displayRandomColor();
function getRandomColor() {
r = Math.floor(Math.random() * 256);
g = Math.floor(Math.random() * 256);
b = Math.floor(Math.random() * 256);
hexR = r.toString(16);
hexG = g.toString(16);
hexB = b.toString(16);
if (hexR.length == 1) {
hexR = "0" + hexR;
}
if (hexG.length == 1) {
hexG = "0" + hexG;
}
if (hexB.length == 1) {
hexB = "0" + hexB;
}
hexColor = "#" + hexR + hexG + hexB;
return hexColor.toUpperCase();
}
function displayRandomColor() {
myRandomColor = getRandomColor();
}
}, 450);
myRandomColor = 000000;
var context;
var x = Math.floor(Math.random() * window.outerWidth);
var y = Math.floor(Math.random() * window.outerHeight);
var x1 = Math.floor(Math.random() * window.outerWidth);
var y1 = Math.floor(Math.random() * window.outerHeight);
var x2 = Math.floor(Math.random() * window.outerWidth);
var y2 = Math.floor(Math.random() * window.outerHeight);
var x3 = Math.floor(Math.random() * window.outerWidth);
var y3 = Math.floor(Math.random() * window.outerHeight);
var x4 = Math.floor(Math.random() * window.outerWidth);
var y4 = Math.floor(Math.random() * window.outerHeight);
var x5 = Math.floor(Math.random() * window.outerWidth);
var y5 = Math.floor(Math.random() * window.outerHeight);
var x6 = Math.floor(Math.random() * window.outerWidth);
var y6 = Math.floor(Math.random() * window.outerHeight);
var x7 = Math.floor(Math.random() * window.outerWidth);
var y7 = Math.floor(Math.random() * window.outerHeight);
var x8 = Math.floor(Math.random() * window.outerWidth);
var y8 = Math.floor(Math.random() * window.outerHeight);
var x9 = Math.floor(Math.random() * window.outerWidth);
var y9 = Math.floor(Math.random() * window.outerHeight);
var dx = Math.floor(Math.random() * 15);
var dy = Math.floor(Math.random() * 15);
var dx1 = Math.floor(Math.random() * 15);
var dy1 = Math.floor(Math.random() * 15);
var dx2 = Math.floor(Math.random() * 15);
var dy2 = Math.floor(Math.random() * 15);
var dx3 = Math.floor(Math.random() * 15);
var dy3 = Math.floor(Math.random() * 15);
var dx4 = Math.floor(Math.random() * 15);
var dy4 = Math.floor(Math.random() * 15);
var dx5 = Math.floor(Math.random() * 15);
var dy5 = Math.floor(Math.random() * 15);
var dx6 = Math.floor(Math.random() * 15);
var dy6 = Math.floor(Math.random() * 15);
var dx7 = Math.floor(Math.random() * 15);
var dy7 = Math.floor(Math.random() * 15);
var dx8 = Math.floor(Math.random() * 15);
var dy8 = Math.floor(Math.random() * 15);
var dx9 = Math.floor(Math.random() * 15);
var dy9 = Math.floor(Math.random() * 15);
function init() {
context = myCanvas.getContext('2d');
setInterval(draw, 10);
}
function draw() {
context.clearRect(0, 0, window.outerWidth, window.outerHeight);
context.beginPath();
context.fillStyle = myRandomColor;
context.font = "64px fixedsys";
context.fillText("nerd", x, y);
context.closePath();
context.beginPath();
context.fillStyle = myRandomColor;
context.font = "64px fixedsys";
context.fillText("nerd", x1, y1);
context.closePath();
context.fill();
context.beginPath();
context.fillStyle = myRandomColor;
context.font = "64px fixedsys";
context.fillText("nerd", x2, y2);
context.closePath();
context.beginPath();
context.fillStyle = myRandomColor;
context.font = "64px fixedsys";
context.fillText("nerd", x3, y3);
context.closePath();
context.beginPath();
context.fillStyle = myRandomColor;
context.font = "64px fixedsys";
context.fillText("nerd", x4, y4);
context.closePath();
context.fill();
context.beginPath();
context.fillStyle = myRandomColor;
context.font = "64px fixedsys";
context.fillText("nerd", x5, y5);
context.closePath();
context.beginPath();
context.fillStyle = myRandomColor;
context.font = "64px fixedsys";
context.fillText("nerd", x6, y6);
context.closePath();
context.beginPath();
context.fillStyle = myRandomColor;
context.font = "64px fixedsys";
context.fillText("nerd", x7, y7);
context.closePath();
context.fill();
context.beginPath();
context.fillStyle = myRandomColor;
context.font = "64px fixedsys";
context.fillText("nerd", x8, y8);
context.closePath();
context.beginPath();
context.fillStyle = myRandomColor;
context.font = "64px fixedsys";
context.fillText("nerd", x9, y9);
context.closePath();
if (x < 0 || x > window.outerWidth) dx = -dx;
if (y < 0 || y > window.outerHeight) dy = -dy;
x += dx;
y += dy;
if (x1 < 0 || x1 > window.outerWidth) dx1 = -dx1;
if (y1 < 0 || y1 > window.outerHeight) dy1 = -dy1;
x1 += dx1;
y1 += dy1;
if (x2 < 0 || x2 > window.outerWidth) dx2 = -dx2;
if (y2 < 0 || y2 > window.outerHeight) dy2 = -dy2;
x2 += dx2;
y2 += dy2;
if (x3 < 0 || x3 > window.outerWidth) dx3 = -dx3;
if (y3 < 0 || y3 > window.outerHeight) dy3 = -dy3;
x3 += dx3;
y3 += dy3;
if (x4 < 0 || x4 > window.outerWidth) dx4 = -dx4;
if (y4 < 0 || y4 > window.outerHeight) dy4 = -dy4;
x4 += dx4;
y4 += dy4;
if (x5 < 0 || x5 > window.outerWidth) dx5 = -dx5;
if (y5 < 0 || y5 > window.outerHeight) dy5 = -dy5;
x5 += dx5;
y5 += dy5;
if (x6 < 0 || x6 > window.outerWidth) dx6 = -dx6;
if (y6 < 0 || y6 > window.outerHeight) dy6 = -dy6;
x6 += dx6;
y6 += dy6;
if (x7 < 0 || x7 > window.outerWidth) dx7 = -dx7;
if (y7 < 0 || y7 > window.outerHeight) dy7 = -dy7;
x7 += dx7;
y7 += dy7;
if (x8 < 0 || x8 > window.outerWidth) dx8 = -dx8;
if (y8 < 0 || y8 > window.outerHeight) dy8 = -dy8;
x8 += dx8;
y8 += dy8;
if (x9 < 0 || x9 > window.outerWidth) dx9 = -dx9;
if (y9 < 0 || y9 > window.outerHeight) dy9 = -dy9;
x9 += dx9;
y9 += dy9;
}
<html>
<body onLoad="init();"> <canvas id="myCanvas" style='position: absolute; left: 0px; top: 0px;'>
</canvas>
<div id="colortext">
</div>
<script src="scripts/suchcolor.js"></script>
<script>
(function() {
var htmlCanvas = document.getElementById('myCanvas'),
context = htmlCanvas.getContext('2d');
initialize();
function initialize() {
window.addEventListener('resize', resizeCanvas, false);
resizeCanvas();
}
function redraw() {
context.strokeStyle = 'blue';
context.lineWidth = '5';
context.strokeRect(0, 0, window.innerWidth, window.innerHeight);
}
function resizeCanvas() {
htmlCanvas.width = window.innerWidth;
htmlCanvas.height = window.innerHeight;
redraw();
}
})();
</script>
</body>
</html>
现在我该怎么办?
最佳答案
您可以通过引入数组和循环来大幅改进您的代码。创建一个空数组 varboxs = []
并将 box 放入其中。每个盒子由位置、尺寸和速度组成,例如:
var box = {x: 0, y: 0, width: 10, height: 10, dx: 1, dy: 1};
boxes.push(box);
然后您可以使用 for 循环迭代所有框:
for (var i = 0; i < boxes.length; i++) {
var box = boxes[i];
// Do something with the i-th box...
}
处理盒子与盒子的碰撞可以用几行代码完成,但它不会很健壮。 IE。在检测框重叠并通过直接更新框速度解决碰撞之前,每轮只能更新框位置几个像素。可能需要几帧才能解决多框碰撞,并且在此期间框可以明显重叠。对于处理堆叠、对象之间的力并具有更好的集成机制的更强大的物理,您应该寻找经过测试的 2D 盒子物理库。
对于物理模拟和动画来说,计时非常重要。对于动画,you should use requestAnimationFrame
而不是 setInterval
。由于这两种方法都不能保证恒定的时间步长,因此您需要计算两次物理更新之间传递的时间 dt
,并将该时间步长 dt
上的速度积分(= 相乘)以获取新职位。
用“穷人的物理学”重新组织代码:
// Return random RGB color string:
function randomColor() {
var hex = Math.floor(Math.random() * 0x1000000).toString(16);
return "#" + ("000000" + hex).slice(-6);
}
// Poor man's box physics update for time step dt:
function doPhysics(boxes, width, height, dt) {
for (let i = 0; i < boxes.length; i++) {
var box = boxes[i];
// Update positions:
box.x += box.dx * dt;
box.y += box.dy * dt;
// Handle boundary collisions:
if (box.x < 0) {
box.x = 0;
box.dx = -box.dx;
} else if (box.x + box.width > width) {
box.x = width - box.width;
box.dx = -box.dx;
}
if (box.y < 0) {
box.y = 0;
box.dy = -box.dy;
} else if (box.y + box.height > height) {
box.y = height - box.height;
box.dy = -box.dy;
}
}
// Handle box collisions:
for (let i = 0; i < boxes.length; i++) {
for (let j = i + 1; j < boxes.length; j++) {
var box1 = boxes[i];
var box2 = boxes[j];
var dx = Math.abs(box1.x - box2.x);
var dy = Math.abs(box1.y - box2.y);
// Check for overlap:
if (2 * dx < (box1.width + box2.width ) &&
2 * dy < (box1.height + box2.height)) {
// Swap dx if moving towards each other:
if ((box1.x > box2.x) == (box1.dx < box2.dx)) {
var swap = box1.dx;
box1.dx = box2.dx;
box2.dx = swap;
}
// Swap dy if moving towards each other:
if ((box1.y > box2.y) == (box1.dy < box2.dy)) {
var swap = box1.dy;
box1.dy = box2.dy;
box2.dy = swap;
}
}
}
}
}
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
// Initialize random boxes:
var boxes = [];
for (var i = 0; i < 10; i++) {
var box = {
x: Math.floor(Math.random() * canvas.width),
y: Math.floor(Math.random() * canvas.height),
width: 50,
height: 20,
dx: (Math.random() - 0.5) * 0.2,
dy: (Math.random() - 0.5) * 0.2
};
boxes.push(box);
}
// Initialize random color and set up interval:
var color = randomColor();
setInterval(function() {
color = randomColor();
}, 450);
// Update physics at fixed rate:
var last = performance.now();
setInterval(function(time) {
var now = performance.now();
doPhysics(boxes, canvas.width, canvas.height, now - last);
last = now;
}, 50);
// Draw animation frames at optimal frame rate:
function draw(now) {
context.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < boxes.length; i++) {
var box = boxes[i];
// Interpolate position:
var x = box.x + box.dx * (now - last);
var y = box.y + box.dy * (now - last);
context.beginPath();
context.fillStyle = color;
context.font = "20px fixedsys";
context.textBaseline = "hanging";
context.fillText("nerd", x, y);
context.closePath();
}
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
<canvas id="canvas"></canvas>
关于javascript - 使 Canvas 文本对象相互反弹并由数组创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43726536/
表架构 DROP TABLE bla; CREATE TABLE bla (id INTEGER, city INTEGER, year_ INTEGER, month_ INTEGER, val I
我需要拆分字符串/或从具有以下结构的字符串中获取更容易的子字符串。 字符串将来自 window.location.pathname 或 window.location.href,看起来像 text/n
每当将对象添加到数组中时,我都会尝试更新 TextView ,并在 TextView 中显示该文本,如下所示: "object 1" "object 2" 问题是,每次将新对象添加到数组时,它都会覆盖
我目前正在寻找使用 Java 读取网站可见文本并将其存储为纯文本字符串的方法。 换句话说,我想转换成这样: Hello stupid World进入“ Hello World ” 或者类似的东西 Un
我正在尝试以文本和 HTML 格式发送电子邮件,但无法正确发送正确的 header 。特别是,我想设置 Content-Type header ,但我找不到如何为 html 和文本部分单独设置它。 这
我尝试了上面的代码,但我无法绑定(bind)文本,我怎样才能将资源内部文本 bloc
我刚刚完成了 Space Shooter 教程,由于没有 GUIText 对象,所以我创建了 UI.Text 对象并进行了相应的编码。它在统一播放器中有效,但在构建 Web 应用程序后无效。我花了一段
我有这个代码: - (IBAction)setButtonPressed:(id)sender { NSUserDefaults *sharedDefaults = [[NSUserDefau
抱歉标题含糊不清,但我想不出我想在标题中做什么。无论如何,对于图像上的文本,我使用了 JLabel 文本并将其添加到图标中。 JLabel icon = new JLabel(new Imag
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我在将 Twitter 嵌入到我从 HTML 5 转换的 wordpress 运行网站时遇到问题。 我遇到的问题是推文不是我的自定义字体... 这是我无法使用任何 css 定位的 HTML 代码,我正
我正在尝试找到解决由于使用以下形式的代码而导致的冗余字符串连接问题的最佳方法: logger.debug("Entering loop, arg is: " + arg) // @1 在大多数情况下,
我写了这个测试 @Test public void removeRequestTextFromRouteError() throws Exception { String input = "F
我目前正在创建一个正则表达式来拆分所有匹配以下格式的字符串:&[文本],并且需要获取文本。字符串可能类似于:something &[text] &[text] everything &[text] 等
有没有办法将标题文本从一个词变形为另一个词,同时保留两个词中使用的字母?我看过的许多 css 文本动画大多是视觉的,很少有旋转整个单词的。 我想要做的是从一个词过渡,例如“BEACH”到“CHANGE
总结matplotlib绘图如何设置坐标轴刻度大小和刻度。 上代码: ?
我在容器 (1) 中创建了容器 (2)。你能帮忙如何向容器(1)添加文本吗?下面是我的代码 return Scaffold( body: Padding( padding: c
我似乎找不到任何人或任何人这样做过。我试图限制我们使用的图像数量,并想创建一个带有渐变作为其“颜色”的文本,并在其周围设置渐变轮廓/描边 到目前为止,我还没有看到任何将两者结合在一起的东西。 我可以自
我正在为视频游戏暗黑破坏神 2 使用 discord.py 构建一个不和谐机器人。其中一项功能要求机器人从暗黑破坏神 2 屏幕截图中提取项目的名称和属性。我目前正在为此使用 pytesseract,但
我很难弄清楚如何旋转 strip.text theme 中的属性来自 ggplot2 .我使用的是 R 版本 3.4.2 和 ggplot2 版本 2.2.1。 以下是 MWE 的数据。 > dput
我是一名优秀的程序员,十分优秀!