forked from sparkbox/logo-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.html
157 lines (144 loc) · 4.53 KB
/
canvas.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Canvas Logo</title>
<link rel="stylesheet" href="all.css">
<style>
.c {
display: block;
border: 0.5px solid #ccc;
margin: 0 auto;
}
</style>
</head>
<body>
<h1 class="title">Canvas Logo</h1>
<canvas id="c" class="c" width="250" height="250"></canvas>
<p class="caption">Go ahead. Inspect me.</p>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
var image = null;
function loadAndDraw() {
image = new Image();
image.src = "images/canvas-bg.jpg";
image.addEventListener('load', drawLogo);
}
function drawLogo() {
var canvas = document.getElementById("c");
var ctx = canvas.getContext("2d");
var pi = 3.14159;
var strokeWidth = 9;
var bgColor = "#404040";
var strokeColor = "#e3e3e3";
// Draw canvas background.
ctx.drawImage(image, 0, 0);
// Logo background.
ctx.fillStyle = bgColor;
roundRect(ctx, 32, 32, 186, 186, 6, true, false);
// "B" Shapes
ctx.beginPath();
ctx.lineCap="square";
ctx.lineWidth = strokeWidth;
ctx.strokeStyle = strokeColor;
ctx.moveTo(80, 80);
ctx.lineTo(155, 80);
ctx.arc(155, 94, 14, 1.5 * pi, 0.5 * pi);
ctx.moveTo(155, 108);
ctx.lineTo(80, 108);
ctx.lineTo(80, 80);
ctx.stroke();
ctx.beginPath();
ctx.lineCap="square";
ctx.lineWidth = strokeWidth;
ctx.strokeStyle = strokeColor;
ctx.moveTo(80, 140);
ctx.lineTo(155, 140);
ctx.arc(155, 154, 14, 1.5 * pi, 0.5 * pi);
ctx.moveTo(155, 168);
ctx.lineTo(80, 168);
ctx.lineTo(80, 140);
ctx.stroke();
// Zig Zags
//
// We have to double the stroke width to compensate for the
// fill covering up the inner stroke.
ctx.beginPath();
ctx.lineWidth = 2 * strokeWidth;
ctx.strokeStyle = strokeColor;
ctx.moveTo(148, 76);
ctx.lineTo(80, 119);
ctx.lineTo(125, 119);
ctx.closePath();
ctx.stroke();
ctx.fill();
ctx.beginPath();
ctx.lineWidth = 2 * strokeWidth;
ctx.strokeStyle = strokeColor;
ctx.moveTo(96, 170);
ctx.lineTo(158, 129);
ctx.lineTo(115, 129);
ctx.closePath();
ctx.stroke();
ctx.fill();
// Fixups
ctx.beginPath();
ctx.lineWidth = strokeWidth - 2;
ctx.strokeStyle = bgColor;
ctx.moveTo(115, 116);
ctx.lineTo(145, 116);
ctx.closePath();
ctx.stroke();
ctx.fill();
ctx.beginPath();
ctx.lineWidth = strokeWidth - 2;
ctx.strokeStyle = bgColor;
ctx.moveTo(90, 132);
ctx.lineTo(120, 132);
ctx.closePath();
ctx.stroke();
ctx.fill();
}
/**
* Draws a rounded rectangle using the current state of the canvas.
* If you omit the last three params, it will draw a rectangle
* outline with a 5 pixel border radius
* @param {CanvasRenderingContext2D} ctx
* @param {Number} x The top left x coordinate
* @param {Number} y The top left y coordinate
* @param {Number} width The width of the rectangle
* @param {Number} height The height of the rectangle
* @param {Number} radius The corner radius. Defaults to 5;
* @param {Boolean} fill Whether to fill the rectangle. Defaults to false.
* @param {Boolean} stroke Whether to stroke the rectangle. Defaults to true.
*/
function roundRect(ctx, x, y, width, height, radius, fill, stroke) {
if (typeof stroke == "undefined" ) {
stroke = true;
}
if (typeof radius === "undefined") {
radius = 5;
}
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
if (stroke) {
ctx.stroke();
}
if (fill) {
ctx.fill();
}
}
loadAndDraw();
});
</script>
</body>
</html>