簡體   English   中英

clearRect不清除畫布中的任何內容

[英]clearRect not clearing anything in canvas

我下面的代碼用於Chris Courses的“圓周運動”教程,但是我不知道為什么我的clearRect無法正常工作。 一定是我沒有看到的東西,現在我還有另外兩個畫布動畫正在工作,但是這個動畫無法清除矩形,這讓我發瘋。

感謝任何有時間幫助的人!

 function spirals() { const canvas2 = document.getElementById('mycanvas'); canvas2.width = document.getElementById('mycanvas').scrollWidth; canvas2.height = document.getElementById('mycanvas').scrollHeight; const c2 = canvas2.getContext('2d'); const spiralColorArray = [ '#ff0000', '#00ff00', '#0000ff' ]; addEventListener('resize', () => { canvas2.width = document.getElementById('mycanvas').scrollWidth; canvas2.height = document.getElementById('mycanvas').scrollHeight; init(); }); function SpinnerIcon(h, v, radius, color) { this.h = h; this.v = v; this.color = color; this.radius = radius; this.update = () => { this.h += 1; this.draw(); }; this.draw = () => { c2.beginPath; c2.arc(this.h, this.v, this.radius, 0, Math.PI*2, false); c2.fillStyle = this.color; c2.fill(); c2.closePath(); } } function init() { spinnerArray = []; for(let i=0; i < 1; i++) { spinnerArray.push(new SpinnerIcon(canvas2.width/2, canvas2.height/2, 5, 'red')) } } let spinnerArray; function animate() { requestAnimationFrame(animate); c2.clearRect(0, 0, canvas2.width, canvas2.height); spinnerArray.forEach(parti => { parti.update(); }) } init(); animate(); } spirals(); 
 #mycanvas { background: blue; } 
 <canvas id="mycanvas" width="500" height="500"> 

您使用c2.beginPath ()的行丟失了(),應為c2.beginPath(); 因為它是一個功能 當未調用beginPath時,clearPath將不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM