canvas繪制

在html創(chuàng)建一個canvas畫布
<canvas id="canvas" ref="Canvas" />

使用js語句繪制畫布

const canvas = ref<any | null>(null);

const draw = (value: string) => {
  // 創(chuàng)建 context 對象
  const ctx = canvas.value?.getContext('2d');

 // 設(shè)置畫布寬高
  canvas.value.width = 200;
  canvas.value.height = 100;
  // 創(chuàng)建一個線性漸變
  const gradient = ctx.createLinearGradient(0,0,200,0);

  // 設(shè)置漸變顏色
  gradient.addColorStop(0, "red");
  gradient.addColorStop(1, "white");
  // 設(shè)置fillStyle為當(dāng)前的漸變對象,并填充
  ctx.fillStyle = gradient;
  ctx.fillRect(10,10,250,40);

  // 設(shè)置字體樣式和內(nèi)容
  ctx.font="16px Arial";
  ctx.fillStyle = 'black';
  ctx.fillText("Hello World",8,10)
};
展示效果

其他方法:

fillText(text,x,y):在畫布上繪制實(shí)心文本;

strokeText(text,x,y):在畫布上繪制空心文本;

fillStyle():設(shè)置或返回用于填充繪畫的顏色、漸變或模式

fillRect(x,y,width,height) :繪制“被填充”的矩形.如 fillRect 方法擁有參數(shù) (0,0,200,100)。即在畫布上繪制 200x100 的矩形,從左上角開始 (0,0)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容