Ich habe den folgenden Code in HTML:
<canvas id="myCanvas" width =800 height=800>
Ich möchte anstelle der width
als 800
die JavaScript-Funktion getWidth()
aufrufen, um die Breite z.
<canvas id="myCanvas" width =getWidth() height=800>
Was ist die richtige Syntax dafür? Weil das, was ich tue, nicht funktioniert.
Sie können die Breite wie folgt einstellen:
function draw() {
var ctx = (a canvas context);
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
//...drawing code...
}
function setWidth(width) {
var canvas = document.getElementById("myCanvas");
canvas.width = width;
}
Versuche dies:
var setCanvasSize = function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
Versuche dies:
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
xS = w.innerWidth || e.clientWidth || g.clientWidth,
yS = w.innerHeight|| e.clientHeight|| g.clientHeight;
alert(xS + ' × ' + yS);
document.write ('')
funktioniert für iframe und gut.