+ <title>Draw Rect</title>
+ <canvas id="canvas1" width="100" height="100"></canvas>
+ <canvas id="canvas2" width="100" height="100"></canvas>
+ <canvas id="canvas3" width="100" height="100"></canvas>
+ <canvas id="canvas4" width="100" height="100"></canvas>
+ var onload = function() {
+ draw1(document.getElementById('canvas1'));
+ draw2(document.getElementById('canvas2'));
+ draw3(document.getElementById('canvas3'));
+ draw4(document.getElementById('canvas4'));
+ var draw1 = function(canvas) {
+ var ctx = canvas.getContext('2d');
+ var canvasWidth = canvas.getAttribute('width');
+ var canvasHeight = canvas.getAttribute('height');
+ ctx.strokeRect(0, 0, canvasWidth, canvasHeight);
+ [offsetX, canvasHeight - offsetY - rectHeight],
+ [canvasWidth - offsetX - rectWidth, offsetY],
+ [canvasWidth - offsetX - rectWidth, canvasHeight - offsetY - rectHeight]
+ ].forEach(function(a) {
+ ctx.strokeRect(x, y, rectWidth, rectHeight);
+ var draw2 = function(canvas) {
+ var ctx = canvas.getContext('2d');
+ var canvasWidth = canvas.getAttribute('width');
+ var canvasHeight = canvas.getAttribute('height');
+ ctx.strokeRect(0, 0, canvasWidth, canvasHeight);
+ [canvasWidth, 0, -1, 1], // 右上
+ [0, canvasHeight, 1, -1], // 左下
+ [canvasWidth, canvasHeight, -1, -1] // 右下
+ ].forEach(function(a) {
+ ctx.translate(translateX, translateY);
+ ctx.scale(scaleX, scaleY);
+ ctx.strokeRect(offsetX, offsetY, rectWidth, rectHeight);
+ var draw3 = function(canvas) {
+ var ctx = canvas.getContext('2d');
+ var canvasWidth = canvas.getAttribute('width');
+ var canvasHeight = canvas.getAttribute('height');
+ ctx.strokeRect(0, 0, canvasWidth, canvasHeight);
+ [canvasWidth, 0, -1, 1], // 右上
+ [0, canvasHeight, 1, -1], // 左下
+ [canvasWidth, canvasHeight, -1, -1] // 右下
+ ].forEach(function(a) {
+ ctx.translate(translateX, translateY);
+ ctx.scale(scaleX, scaleY);
+ myRect1(ctx, offsetX, offsetY, rectWidth, rectHeight);
+ var draw4 = function(canvas) {
+ var ctx = canvas.getContext('2d');
+ var canvasWidth = canvas.getAttribute('width');
+ var canvasHeight = canvas.getAttribute('height');
+ ctx.strokeRect(0, 0, canvasWidth, canvasHeight);
+ [canvasWidth, 0, -1, 1], // 右上
+ [0, canvasHeight, 1, -1], // 左下
+ [canvasWidth, canvasHeight, -1, -1] // 右下
+ ].forEach(function(a) {
+ ctx.translate(translateX, translateY);
+ ctx.scale(scaleX, scaleY);
+ myRect2(ctx, offsetX, offsetY, rectWidth, rectHeight);
+ function myRect1(ctx, x, y, width, height) {
+ ctx.lineTo(x + width, y);
+ ctx.lineTo(x + width, y + height);
+ ctx.lineTo(x, y + height);
+ function myRect2(ctx, x, y, width, height) {
+ ctx.lineTo(width, height);
+ window.addEventListener('load', onload, false);