2020-06-22-Canvas笔记02-矩形绘制

2020-06-22  本文已影响0人  cherry_liulei

矩形的绘制

Canvas的API 提供了3个方法,分别用于矩形的清除、描边及填充

const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');

context.strokeRect(10, 10, 100, 100);
绘制矩形.png
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');

context.strokeRect(10, 10, 100, 100);

context.fillStyle = 'orange';
context.fillRect(20, 20, 100, 100);
填充矩形.png
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');

context.strokeRect(10, 10, 100, 100);

context.fillStyle = 'orange';
context.fillRect(20, 20, 100, 100);

context.canvas.onmousedown = function(e) {
  context.clearRect(0, 0, 100, 100);
}
清除矩形.png
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
context.lineWidth = 30;
context.strokeRect(50, 50, 400, 200);
设置lineWidth.png
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
context.lineJoin = 'round';
context.lineWidth = 30;
context.strokeRect(50, 50, 400, 200);
绘制矩形的lineJoin-bevel.png 绘制矩形的lineJoin-round.png 绘制矩形的lineJoin-miter.png
上一篇下一篇

猜你喜欢

热点阅读