-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurvedPatterns.html
More file actions
329 lines (259 loc) · 10.3 KB
/
curvedPatterns.html
File metadata and controls
329 lines (259 loc) · 10.3 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<!doctype HTML>
<html>
<head>
<title>instant Art</title>
<link href="https://fonts.googleapis.com/css?family=Alfa+Slab+One&display=swap" rel="stylesheet">
<style>
#title {
font-family: 'Alfa Slab One', sans-serif;
font-size: 50px;
color: black;
background-color: white;
letter-spacing: -2px;
margin-top: 1000px;
}
span{
transition: all 0.5s ease-in-out;
}
a:hover{
cursor: pointer;
}
center{
margin-top: 50px;
}
canvas{
visibility:hidden;
}
#pic{
position: fixed;
}
</style>
</head>
<body>
<center><a id="title" onClick="colorPixels()">instant patterns</a>
<div id="container"><canvas id="canvas" height="600px" width="600px"></canvas></div>
<canvas id="pic" height="600px" width="600px"></canvas>
</center>
<script>
document.addEventListener('mousedown', function (event) {
if (event.detail > 1) {
event.preventDefault();
}
}, false);
var c=document.getElementById("canvas");
var ctx=c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
function colorPixels(){
var x = 0;
var y = 0;
ctx.clearRect(0, 0, c.width, c.height);
var numOfPoints = Math.floor(Math.random() * (8 - 4 + 1)) + 4;
var points = [];
//first point
points.push(0); //x coordinate
points.push(Math.floor(Math.random() * (c.height + 1))); //y coordinate
for(var i = 0; i < numOfPoints-2;i++){ //adding points to the array
points.push(Math.floor(Math.random() * (c.width + 1))); //x coordinate
points.push(Math.floor(Math.random() * (c.height + 1))); //y coordinate
}
//last point
points.push(c.width); //x coordinate
points.push(Math.floor(Math.random() * (c.height + 1))); //y coordinate
ctx.lineWidth = 10;
ctx.fillStyle = '#'+ Math.random().toString(16).slice(-6); //filling background
ctx.fillRect(0,0,c.width,c.height);
ctx.fillStyle = '#'+ Math.random().toString(16).slice(-6); //random color for the drawing
ctx.strokeStyle = ctx.fillStyle;
drawCurve(ctx, points);
ctx.lineTo(c.width,c.height); //in order to fill, closing the borders
ctx.lineTo(0,c.height);
ctx.lineTo(points[0],points[1]);
ctx.stroke(); //drawing
ctx.fill();
var temp = document.getElementById("pic");
var tempContext = temp.getContext("2d");
//drawing the pattern block in anther canvas
switch(Math.floor(Math.random()*4)){
case 0:
//normal, no flip
tempContext.drawImage(c,0,0,c.width/2,c.width/2);
tempContext.drawImage(c,c.width/2,0,c.width/2,c.width/2);
tempContext.drawImage(c,0,c.width/2,c.width/2,c.width/2);
tempContext.drawImage(c,c.width/2,c.width/2,c.width/2,c.width/2);
break;
case 1:
//simple horizontal flip
tempContext.save();
tempContext.drawImage(c, 0,0,temp.width/2,temp.width/2);
tempContext.scale(-1, 1);
tempContext.drawImage(c, -temp.width,0,temp.width/2,temp.width/2);
tempContext.drawImage(c, -temp.width,temp.width/2,temp.width/2,temp.width/2);
tempContext.scale(-1, 1);
tempContext.drawImage(c, 0,temp.width/2,temp.width/2,temp.width/2);
tempContext.restore();
break;
case 2:
//simple horizontal flip & slide
tempContext.save();
tempContext.drawImage(c, 0,0,temp.width/2,temp.width/2);
tempContext.scale(-1, 1);
tempContext.drawImage(c, -temp.width,0,temp.width/2,temp.width/2);
tempContext.drawImage(c, -temp.width/2,temp.width/2,temp.width/2,temp.width/2);
tempContext.scale(-1, 1);
tempContext.drawImage(c, temp.width/2,temp.width/2,temp.width/2,temp.width/2);
tempContext.restore();
break;
case 3:
//horizontal & vertical flip
tempContext.save();
tempContext.drawImage(c, 0,0,temp.width/2,temp.width/2);
tempContext.scale(-1, 1);
tempContext.drawImage(c, -temp.width,0,temp.width/2,temp.width/2);
tempContext.scale(-1,-1);
tempContext.drawImage(c, 0,-temp.width,temp.width/2,temp.width/2);
tempContext.scale(-1,1);
tempContext.drawImage(c, -temp.width,-temp.width,temp.width/2,temp.width/2);
tempContext.restore();
}
ctx.clearRect(0,0,c.height,c.width);
for(var k = 0; k < 2; k++){
for(var i = 0; i < 2;i++){
ctx.drawImage(temp,300*i,k*300,300,300);
}
}
setBackground();
}
function setBackground(){
var img = c.toDataURL();
document.body.style.background = "url(" +img+ ")";
}
function drawCurve(ctx, ptsa, tension, isClosed, numOfSegments, showPoints) {
showPoints = showPoints ? showPoints : false;
ctx.beginPath();
drawLines(ctx, getCurvePoints(ptsa, tension, isClosed, numOfSegments));
if (showPoints) {
ctx.stroke();
ctx.beginPath();
for(var i=0;i<ptsa.length-1;i+=2) {
ctx.rect(ptsa[i] - 2, ptsa[i+1] - 2, 4, 4);
}
}
}
function getCurvePoints(pts, tension, isClosed, numOfSegments) {
// use input value if provided, or use a default value
tension = (typeof tension != 'undefined') ? tension : 0.5;
isClosed = isClosed ? isClosed : false;
numOfSegments = numOfSegments ? numOfSegments : 16;
var _pts = [], res = [], // clone array
x, y, // our x,y coords
t1x, t2x, t1y, t2y, // tension vectors
c1, c2, c3, c4, // cardinal points
st, t, i; // steps based on num. of segments
// clone array so we don't change the original
//
_pts = pts.slice(0);
// The algorithm require a previous and next point to the actual point array.
// Check if we will draw closed or open curve.
// If closed, copy end points to beginning and first points to end
// If open, duplicate first points to befinning, end points to end
if (isClosed) {
_pts.unshift(pts[pts.length - 1]);
_pts.unshift(pts[pts.length - 2]);
_pts.unshift(pts[pts.length - 1]);
_pts.unshift(pts[pts.length - 2]);
_pts.push(pts[0]);
_pts.push(pts[1]);
}
else {
_pts.unshift(pts[1]); //copy 1. point and insert at beginning
_pts.unshift(pts[0]);
_pts.push(pts[pts.length - 2]); //copy last point and append
_pts.push(pts[pts.length - 1]);
}
// ok, lets start..
// 1. loop goes through point array
// 2. loop goes through each segment between the 2 pts + 1e point before and after
for (i=2; i < (_pts.length - 4); i+=2) {
for (t=0; t <= numOfSegments; t++) {
// calc tension vectors
t1x = (_pts[i+2] - _pts[i-2]) * tension;
t2x = (_pts[i+4] - _pts[i]) * tension;
t1y = (_pts[i+3] - _pts[i-1]) * tension;
t2y = (_pts[i+5] - _pts[i+1]) * tension;
// calc step
st = t / numOfSegments;
// calc cardinals
c1 = 2 * Math.pow(st, 3) - 3 * Math.pow(st, 2) + 1;
c2 = -(2 * Math.pow(st, 3)) + 3 * Math.pow(st, 2);
c3 = Math.pow(st, 3) - 2 * Math.pow(st, 2) + st;
c4 = Math.pow(st, 3) - Math.pow(st, 2);
// calc x and y cords with common control vectors
x = c1 * _pts[i] + c2 * _pts[i+2] + c3 * t1x + c4 * t2x;
y = c1 * _pts[i+1] + c2 * _pts[i+3] + c3 * t1y + c4 * t2y;
//store points in array
res.push(x);
res.push(y);
}
}
return res;
}
function drawLines(ctx, pts) {
ctx.moveTo(pts[0], pts[1]);
for(i=2;i<pts.length-1;i+=2) ctx.lineTo(pts[i], pts[i+1]);
}
function download(){
saveAsPNG(c,'stripes');
}
function saveAsPNG(image, filename){ // No IE <11 support. Chrome URL bug for large images may crash
var anchorElement, event, blob;
function image2Canvas(image){ // converts an image to canvas
function createCanvas(width, height){ // creates a canvas of width height
var can = document.createElement("canvas");
can.width = width;
can.height = height;
return can;
};
var newImage = canvas(img.width, img.height); // create new image
newImage.ctx = newImage.getContext("2d"); // get image context
newImage.ctx.drawImage(image, 0, 0); // draw the image onto the canvas
return newImage; // return the new image
}
if(image.toDataURL === undefined){ // does the image have the toDataURL function
image = image2Canvas(image); // No then convert to canvas
}
// if msToBlob and msSaveBlob then use them to save. IE >= 10
// As suggested by Kaiido
if(image.msToBlob !== undefined && navigator.msSaveBlob !== undefined){
blob = image.msToBlob();
navigator.msSaveBlob(blob, filename + ".png");
return;
}
anchorElement = document.createElement('a'); // Create a download link
anchorElement.href = image.toDataURL(); // attach the image data URL
// check for download attribute
if ( anchorElement.download !== undefined ) {
anchorElement.download = filename + ".png"; // set the download filename
if (typeof MouseEvent === "function") { // does the browser support the object MouseEvent
event = new MouseEvent( // yes create a new mouse click event
"click", {
view : window,
bubbles : true,
cancelable : true,
ctrlKey : false,
altKey : false,
shiftKey : false,
metaKey : false,
button : 0,
buttons : 1,
}
);
anchorElement.dispatchEvent(event); // simulate a click on the download link.
} else
if (anchorElement.fireEvent) { // if no MouseEvent object try fireEvent
anchorElement.fireEvent("onclick");
}
}
}
</script>
</body>
</html>