mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-29 08:11:13 +00:00
Base: Move fun canvas demo JavaScript to seperate file
This will allow this demo to be reused for other tests.
This commit is contained in:
parent
b179d514d9
commit
b21d95bbe3
Notes:
sideshowbarker
2024-07-17 09:42:38 +09:00
Author: https://github.com/MacDue Commit: https://github.com/SerenityOS/serenity/commit/b21d95bbe3 Pull-request: https://github.com/SerenityOS/serenity/pull/14482
|
@ -5,50 +5,9 @@
|
|||
</head>
|
||||
<body>
|
||||
<canvas id=c width=400 height=300></canvas>
|
||||
<script src="fun-canvas.js"></script>
|
||||
<script>
|
||||
c = document.getElementById("c");
|
||||
ctx = c.getContext("2d");
|
||||
ctx.fillStyle = 'black';
|
||||
ctx.fillRect(0, 0, c.width, c.height);
|
||||
|
||||
pressed = false;
|
||||
mouseX = 0;
|
||||
mouseY = 0;
|
||||
|
||||
c.addEventListener("mousedown", function(e) {
|
||||
// mousedown
|
||||
pressed = true;
|
||||
mouseX = e.offsetX;
|
||||
mouseY = e.offsetY;
|
||||
});
|
||||
|
||||
c.addEventListener("mouseup", function() {
|
||||
// mouseup
|
||||
pressed = false;
|
||||
});
|
||||
|
||||
c.addEventListener("mousemove", function(e) {
|
||||
// mousemove
|
||||
mouseX = e.offsetX;
|
||||
mouseY = e.offsetY;
|
||||
});
|
||||
|
||||
function update() {
|
||||
if (pressed) {
|
||||
var r = Math.random() * 255;
|
||||
var g = Math.random() * 255;
|
||||
var b = Math.random() * 255;
|
||||
var color = "rgb(" + ~~r + "," + ~~g + "," + ~~b + ")";
|
||||
ctx.fillStyle = color;
|
||||
const spread = 35;
|
||||
var x = mouseX + (Math.random() * spread) - (spread / 2);
|
||||
var y = mouseY + (Math.random() * spread) - (spread / 2);
|
||||
var size = Math.random() * 8;
|
||||
ctx.fillRect(x, y, size, size);
|
||||
}
|
||||
}
|
||||
|
||||
setInterval(update, 20);
|
||||
makeFunCanvas("c")
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
46
Base/res/html/misc/fun-canvas.js
Normal file
46
Base/res/html/misc/fun-canvas.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
const makeFunCanvas = canvasId => {
|
||||
c = document.getElementById(canvasId);
|
||||
|
||||
ctx = c.getContext("2d");
|
||||
ctx.fillStyle = "black";
|
||||
ctx.fillRect(0, 0, c.width, c.height);
|
||||
|
||||
pressed = false;
|
||||
mouseX = 0;
|
||||
mouseY = 0;
|
||||
|
||||
c.addEventListener("mousedown", function (e) {
|
||||
// mousedown
|
||||
pressed = true;
|
||||
mouseX = e.offsetX;
|
||||
mouseY = e.offsetY;
|
||||
});
|
||||
|
||||
c.addEventListener("mouseup", function () {
|
||||
// mouseup
|
||||
pressed = false;
|
||||
});
|
||||
|
||||
c.addEventListener("mousemove", function (e) {
|
||||
// mousemove
|
||||
mouseX = e.offsetX;
|
||||
mouseY = e.offsetY;
|
||||
});
|
||||
|
||||
function update() {
|
||||
if (pressed) {
|
||||
var r = Math.random() * 255;
|
||||
var g = Math.random() * 255;
|
||||
var b = Math.random() * 255;
|
||||
var color = "rgb(" + ~~r + "," + ~~g + "," + ~~b + ")";
|
||||
ctx.fillStyle = color;
|
||||
const spread = 35;
|
||||
var x = mouseX + Math.random() * spread - spread / 2;
|
||||
var y = mouseY + Math.random() * spread - spread / 2;
|
||||
var size = Math.random() * 8;
|
||||
ctx.fillRect(x, y, size, size);
|
||||
}
|
||||
}
|
||||
|
||||
setInterval(update, 20);
|
||||
};
|
Loading…
Reference in a new issue