我在我的网站上有一个画布元素,它在桌面浏览器和安卓设备上都能完美渲染,但在iOS设备(iPad,iPhone)上无法渲染 - 无论是Safari还是Chrome。 我正在使用Materialize作为我的CSS框架。
我的代码需要添加什么吗?
var next; //initialize for interval
//paint random colored pattern on profile screen
function paintCanvas() {
const c = document.querySelector("#canvas");
const ctx = c.getContext("2d");
c.width = window.outerWidth;
const width = c.width;
const height = 612; //canvas height
const min = 0;
const max = width;
const yMid = height/2;
var y1 = yMid;
var y2 = yMid;
function getRandomColor(){
let r = Math.floor(Math.random()*256);
let g = Math.floor(Math.random()*256);
let b = Math.floor(Math.random()*256);
let color = `rgba(${r}, ${g}, ${b}, 0.5)`;
return color;
}
var x = 0;
function paint() {
if (x==(max/2)-2) {
clearInterval(next);
}
ctx.lineWidth = 4; //sets width of line
ctx.strokeStyle = getRandomColor(); //assigns random color
ctx.beginPath(); //start line
ctx.moveTo(x,y1); //moves the origin
ctx.lineTo(max-x,y2); //go to the bottom right corner
ctx.moveTo(x, y2);
ctx.lineTo(max-x, y1);
ctx.stroke();
if(y1==0) {
x++;
} else {
y1--;
y2++;
}
}
next = setInterval(paint, 0.05);
}
paintCanvas();
main {
position: relative;
}
#canvas {
position: absolute;
top:0;
left:0;
width: 100%;
z-index: 0;
}
<main id="#top" role="main">
<canvas id="canvas" width="100%" height = "612px"></canvas>
</main>