亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我在畫布右側的繪圖會改變畫布左側的像素?

為什么我在畫布右側的繪圖會改變畫布左側的像素?

肥皂起泡泡 2023-03-24 16:19:26
我正在使用畫布為音頻創建波形圖形均衡器。我注意到畫布上的所有像素都受到我對畫布特定部分所做的筆觸的影響。如果我對畫布的某個區域進行更改,我希望畫布的其他區域不受影響,但我的期望沒有得到滿足。為什么?謝謝。const c = document.getElementById('c');const ctx = c.getContext('2d');const [ r,g,b ] = [0,0,0];const { height } = ctx.canvas;const colorNode = document.getElementById('color');const narrationNode = document.getElementById('narration');var x = 0;ctx.lineWidth = 1;const interval = setInterval(() => {    x++;  if (x > 500) clearInterval(interval);  narrationNode.innerHTML = narration.map(    (narrative, i) => narrative && x >= i        ? `<li>${narrative}</li>`      : ''  ).join('');    const margin = x/5;  const gradient = ctx.createLinearGradient(    0, margin, 0, height-margin  );  gradient.addColorStop(0, `rgba(${r},${g},$,0)`);  gradient.addColorStop(0.5, `rgba(${r},${g},$,1)`);  gradient.addColorStop(1.0, `rgba(${r},${g},$,0)`);  ctx.strokeStyle = gradient;  ctx.moveTo(x, margin);  ctx.lineTo(x, height-margin);  ctx.stroke();    var pixel = ctx.getImageData(25, 5, 1, 1).data;  var rgba = 'rgba(' + pixel[0] + ', ' + pixel[1] +      ', ' + pixel[2] + ', ' + (pixel[3] / 255) + ')';  colorNode.innerHTML = rgba + ', ' + x;},90)
查看完整描述

1 回答

?
Helenr

TA貢獻1780條經驗 獲得超4個贊

您只缺少一個beginPath,請參閱下面的代碼


const c = document.getElementById('c');

const ctx = c.getContext('2d');

const { height } = ctx.canvas;

var x = 0;


const interval = setInterval(() => {  

  x += 2;

  if (x > 500) clearInterval(interval);

  

  margin = x/5;  

  gradient = ctx.createLinearGradient(0, margin, 0, height-margin);

  gradient.addColorStop(0,   `rgba(0,0,0,0)`);

  gradient.addColorStop(0.5, `rgba(0,0,0,1)`);

  gradient.addColorStop(1.0, `rgba(0,0,0,0)`);  

  ctx.strokeStyle = gradient;

  

  ctx.beginPath();

  ctx.moveTo(x, margin);

  ctx.lineTo(x, height-margin);

  ctx.stroke();

  

},10)

<canvas id="c" height="100" width="500"></canvas>


查看完整回答
反對 回復 2023-03-24
  • 1 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號