Snippets
Created by
Takumi IINO
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Draw Rect</title>
</head>
<body>
<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>
<script>
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');
var rectWidth = 20;
var rectHeight = 20;
var offsetX = 10;
var offsetY = 10;
ctx.beginPath();
ctx.strokeRect(0, 0, canvasWidth, canvasHeight);
[
[offsetX, offsetY],
[offsetX, canvasHeight - offsetY - rectHeight],
[canvasWidth - offsetX - rectWidth, offsetY],
[canvasWidth - offsetX - rectWidth, canvasHeight - offsetY - rectHeight]
].forEach(function(a) {
var x = a[0];
var y = a[1];
ctx.beginPath();
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');
var rectWidth = 20;
var rectHeight = 20;
var offsetX = 10;
var offsetY = 10;
ctx.beginPath();
ctx.strokeRect(0, 0, canvasWidth, canvasHeight);
[
[0, 0, 1, 1], // 左上
[canvasWidth, 0, -1, 1], // 右上
[0, canvasHeight, 1, -1], // 左下
[canvasWidth, canvasHeight, -1, -1] // 右下
].forEach(function(a) {
var translateX = a[0];
var translateY = a[1];
var scaleX = a[2];
var scaleY = a[3];
ctx.save();
ctx.translate(translateX, translateY);
ctx.scale(scaleX, scaleY);
ctx.strokeRect(offsetX, offsetY, rectWidth, rectHeight);
ctx.restore();
});
};
var draw3 = function(canvas) {
var ctx = canvas.getContext('2d');
var canvasWidth = canvas.getAttribute('width');
var canvasHeight = canvas.getAttribute('height');
var rectWidth = 20;
var rectHeight = 20;
var offsetX = 10;
var offsetY = 10;
ctx.beginPath();
ctx.strokeRect(0, 0, canvasWidth, canvasHeight);
[
[0, 0, 1, 1], // 左上
[canvasWidth, 0, -1, 1], // 右上
[0, canvasHeight, 1, -1], // 左下
[canvasWidth, canvasHeight, -1, -1] // 右下
].forEach(function(a) {
var translateX = a[0];
var translateY = a[1];
var scaleX = a[2];
var scaleY = a[3];
ctx.save();
ctx.translate(translateX, translateY);
ctx.scale(scaleX, scaleY);
myRect1(ctx, offsetX, offsetY, rectWidth, rectHeight);
ctx.restore();
});
};
var draw4 = function(canvas) {
var ctx = canvas.getContext('2d');
var canvasWidth = canvas.getAttribute('width');
var canvasHeight = canvas.getAttribute('height');
var rectWidth = 20;
var rectHeight = 20;
var offsetX = 10;
var offsetY = 10;
ctx.beginPath();
ctx.strokeRect(0, 0, canvasWidth, canvasHeight);
[
[0, 0, 1, 1], // 左上
[canvasWidth, 0, -1, 1], // 右上
[0, canvasHeight, 1, -1], // 左下
[canvasWidth, canvasHeight, -1, -1] // 右下
].forEach(function(a) {
var translateX = a[0];
var translateY = a[1];
var scaleX = a[2];
var scaleY = a[3];
ctx.save();
ctx.translate(translateX, translateY);
ctx.scale(scaleX, scaleY);
myRect2(ctx, offsetX, offsetY, rectWidth, rectHeight);
ctx.restore();
});
};
function myRect1(ctx, x, y, width, height) {
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + width, y);
ctx.lineTo(x + width, y + height);
ctx.lineTo(x, y + height);
ctx.lineTo(x, y);
ctx.stroke();
}
function myRect2(ctx, x, y, width, height) {
ctx.save();
ctx.translate(x, y);
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(width, 0);
ctx.lineTo(width, height);
ctx.lineTo(0, height);
ctx.lineTo(0, 0);
ctx.stroke();
ctx.restore();
}
window.addEventListener('load', onload, false);
</script>
</body>
</html>
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.