Ich habe vor kurzem angefangen, auf einer Website zu arbeiten und HTML/CCS/JS zu lernen und stieß auf die Idee, dass ich nicht sicher bin, wie ich es ausführen soll. Grundsätzlich möchte ich, dass sich der schwebende Text, der sich derzeit bewegt und von den Fenstergrenzen springt, auch abprallen lässt. Ich dachte auch, dass ein Array gut sein könnte, um zu bestimmen, wie viele Textobjekte ich spawnen möchte. Hier ist meine Website als Referenz, wie der Text springt um http://gmtrash.ga/Canvas Textobjekte abprallen lassen und von einem Array erstellt werden
Und hier ist das JavaScript Kontrolle der Textobjekte und Färbung
setInterval(function() {
\t myContainer = document.getElementById("colortext");
\t displayRandomColor();
\t function getRandomColor() {
\t \t r = Math.floor(Math.random() * 256);
\t \t g = Math.floor(Math.random() * 256);
\t \t b = Math.floor(Math.random() * 256);
\t \t hexR = r.toString(16);
\t \t hexG = g.toString(16);
\t \t hexB = b.toString(16);
\t \t if (hexR.length == 1) {
\t \t \t hexR = "0" + hexR;
\t \t }
\t \t if (hexG.length == 1) {
\t \t \t hexG = "0" + hexG;
\t \t }
\t \t if (hexB.length == 1) {
\t \t \t hexB = "0" + hexB;
\t \t }
\t \t hexColor = "#" + hexR + hexG + hexB;
\t \t return hexColor.toUpperCase();
\t }
\t function displayRandomColor() {
\t \t myRandomColor = getRandomColor();
\t }
}, 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() {
\t context = myCanvas.getContext('2d');
\t setInterval(draw, 10);
}
function draw() {
\t context.clearRect(0, 0, window.outerWidth, window.outerHeight);
\t context.beginPath();
\t context.fillStyle = myRandomColor;
\t context.font = "64px fixedsys";
\t context.fillText("nerd", x, y);
\t context.closePath();
\t context.beginPath();
\t context.fillStyle = myRandomColor;
\t context.font = "64px fixedsys";
\t context.fillText("nerd", x1, y1);
\t context.closePath();
\t context.fill();
\t context.beginPath();
\t context.fillStyle = myRandomColor;
\t context.font = "64px fixedsys";
\t context.fillText("nerd", x2, y2);
\t context.closePath();
\t context.beginPath();
\t context.fillStyle = myRandomColor;
\t context.font = "64px fixedsys";
\t context.fillText("nerd", x3, y3);
\t context.closePath();
\t context.beginPath();
\t context.fillStyle = myRandomColor;
\t context.font = "64px fixedsys";
\t context.fillText("nerd", x4, y4);
\t context.closePath();
\t context.fill();
\t context.beginPath();
\t context.fillStyle = myRandomColor;
\t context.font = "64px fixedsys";
\t context.fillText("nerd", x5, y5);
\t context.closePath();
\t context.beginPath();
\t context.fillStyle = myRandomColor;
\t context.font = "64px fixedsys";
\t context.fillText("nerd", x6, y6);
\t context.closePath();
\t context.beginPath();
\t context.fillStyle = myRandomColor;
\t context.font = "64px fixedsys";
\t context.fillText("nerd", x7, y7);
\t context.closePath();
\t context.fill();
\t context.beginPath();
\t context.fillStyle = myRandomColor;
\t context.font = "64px fixedsys";
\t context.fillText("nerd", x8, y8);
\t context.closePath();
\t context.beginPath();
\t context.fillStyle = myRandomColor;
\t context.font = "64px fixedsys";
\t context.fillText("nerd", x9, y9);
\t context.closePath();
\t if (x < 0 || x > window.outerWidth) dx = -dx;
\t if (y < 0 || y > window.outerHeight) dy = -dy;
\t x += dx;
\t y += dy;
\t if (x1 < 0 || x1 > window.outerWidth) dx1 = -dx1;
\t if (y1 < 0 || y1 > window.outerHeight) dy1 = -dy1;
\t x1 += dx1;
\t y1 += dy1;
\t if (x2 < 0 || x2 > window.outerWidth) dx2 = -dx2;
\t if (y2 < 0 || y2 > window.outerHeight) dy2 = -dy2;
\t x2 += dx2;
\t y2 += dy2;
\t if (x3 < 0 || x3 > window.outerWidth) dx3 = -dx3;
\t if (y3 < 0 || y3 > window.outerHeight) dy3 = -dy3;
\t x3 += dx3;
\t y3 += dy3;
\t if (x4 < 0 || x4 > window.outerWidth) dx4 = -dx4;
\t if (y4 < 0 || y4 > window.outerHeight) dy4 = -dy4;
\t x4 += dx4;
\t y4 += dy4;
\t if (x5 < 0 || x5 > window.outerWidth) dx5 = -dx5;
\t if (y5 < 0 || y5 > window.outerHeight) dy5 = -dy5;
\t x5 += dx5;
\t y5 += dy5;
\t if (x6 < 0 || x6 > window.outerWidth) dx6 = -dx6;
\t if (y6 < 0 || y6 > window.outerHeight) dy6 = -dy6;
\t x6 += dx6;
\t y6 += dy6;
\t if (x7 < 0 || x7 > window.outerWidth) dx7 = -dx7;
\t if (y7 < 0 || y7 > window.outerHeight) dy7 = -dy7;
\t x7 += dx7;
\t y7 += dy7;
\t if (x8 < 0 || x8 > window.outerWidth) dx8 = -dx8;
\t if (y8 < 0 || y8 > window.outerHeight) dy8 = -dy8;
\t x8 += dx8;
\t y8 += dy8;
\t if (x9 < 0 || x9 > window.outerWidth) dx9 = -dx9;
\t if (y9 < 0 || y9 > window.outerHeight) dy9 = -dy9;
\t x9 += dx9;
\t 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>
jetzt wie würde ich über diese gehen?
für die manuelle Schleife –
Blick auf http://phaser.io Abrollen - es umfasst Spielphysik und es ist relativ einfach zu bekommen angefangen mit (Sie brauchen kein OpenGL oder WebGL) – Bob
@le_m Warum? Code wie dieser ist nicht lesbar oder wartbar. Dies kann eine schwerwiegende vorzeitige Optimierung (oder nur Anfängercode) darstellen. –