Ich versuche, dieses Regentropfen-Canvas-Skript 100% Breite und Höhe aufzunehmen, aber nichts, was ich zu tun scheint funktioniert. Ich habe versucht, CSS und Höhe/Breite im Canvas-Bereich zu ändern, aber es ändert nichts oder es funktioniert überhaupt nicht. Das eine Mal, als ich etwas ausprobierte, das es wirklich groß machte, schien es eine seltsame Wirkung auf die Regentropfen zu haben, sie wurden alle verschwommen und viel größer, also musste es die Leinwand tatsächlich gestreckt haben, anstatt sie größer zu machen. Hier ist der Code für das Standard-800x800-Pixel-Canvas.HTML5 Canvas 100% Höhe und Breite
Stil
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
Script für die Leinwand
<script type="text/javascript">
var canvas = null;
var context = null;
var bufferCanvas = null;
var bufferCanvasCtx = null;
var flakeArray = [];
var flakeTimer = null;
var maxFlakes = 200; // Here you may set max flackes to be created
function init() {
canvas = document.getElementById('canvasRain');
context = canvas.getContext("2d");
bufferCanvas = document.createElement("canvas");
bufferCanvasCtx = bufferCanvas.getContext("2d");
bufferCanvasCtx.canvas.width = context.canvas.width;
bufferCanvasCtx.canvas.height = context.canvas.height;
flakeTimer = setInterval(addFlake, 200);
Draw();
setInterval(animate, 30);
}
function animate() {
Update();
Draw();
}
function addFlake() {
flakeArray[flakeArray.length] = new Flake();
if (flakeArray.length == maxFlakes)
clearInterval(flakeTimer);
}
function blank() {
bufferCanvasCtx.fillStyle = "rgba(0,0,0,0.8)";
bufferCanvasCtx.fillRect(0, 0, bufferCanvasCtx.canvas.width, bufferCanvasCtx.canvas.height);
}
function Update() {
for (var i = 0; i < flakeArray.length; i++) {
if (flakeArray[i].y < context.canvas.height) {
flakeArray[i].y += flakeArray[i].speed;
if (flakeArray[i].y > context.canvas.height)
flakeArray[i].y = -5;
flakeArray[i].x += flakeArray[i].drift;
if (flakeArray[i].x > context.canvas.width)
flakeArray[i].x = 0;
}
}
}
function Flake() {
this.x = Math.round(Math.random() * context.canvas.width);
this.y = -10;
this.drift = Math.random();
this.speed = Math.round(Math.random() * 5) + 1;
this.width = (Math.random() * 3) + 2;
this.height = this.width;
}
function Draw() {
context.save();
blank();
for (var i = 0; i < flakeArray.length; i++) {
bufferCanvasCtx.fillStyle = "white";
bufferCanvasCtx.fillRect(flakeArray[i].x, flakeArray[i].y, flakeArray[i].width, flakeArray[i].height);
}
context.drawImage(bufferCanvas, 0, 0, bufferCanvas.width, bufferCanvas.height);
context.restore();
}
</script>
Und schließlich ist hier der Körper
<body onload="init()">
<canvas id="canvasRain" width="800px" height="800px">Canvas Not Supported</canvas>
</body>
Haben Sie versucht, die Einstellung der CSS Höhe und Breite für das Canvas-Element? – aet
@aet Ja, habe ich 100% für beide versucht und es ändert es überhaupt nicht, tatsächlich habe ich auch den falschen Code eingegeben, ich glaube nicht, dass dieser überhaupt funktioniert, lass mich den funktionierenden Standardcode in –
setzen Setzen Sie die Breite und Höhe des Körpers auf 100% und dann die Leinwand auf 100%. Beide mit CSS. – MeiSign