-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwallpaper.js
1287 lines (1079 loc) · 36.1 KB
/
wallpaper.js
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// for our deterministic random number generator
var a = 1664525 * Math.random();
var c = 1013904223;
var seed = 1234;
//function seedDeterministicRandomNumberGenerator(newSeed) {
function seedDRand(newSeed) {
seed = newSeed;
//console.log("seed = " + seed + "\n");
}
function dRandom() {
// define the recurrence relationship
seed = parseInt(a * seed + c) % 982451497;
// return an integer
// Could return a float in (0, 1) by dividing by m
return seed;
}
// helper function which returns an integer from 0 to spread - 1
// for 4 it would be 0, 1, 2, or 3 (not 4)
function dRandomInt(spread) {
return (dRandom() % spread);
}
// returns an integer from min to max (inclusive)
function dRandomIn(min, max) {
return min + dRandom() % (max - min + 1);
}
// return a float in the 0-1 range
function dRandomFloat() {
return dRandom() / 982451497;
}
// return a float in the 0-1 range
function dRandomFloatIn(min, max) {
return min + (max - min) * dRandom() / 982451497;
}
// uses the fibonacci sequence to generate pseudorandom numbers
function fibonacci(a, b) {
var period = Math.pow(10, 6);
//console.log("testing fibonacci sequence. inputs are " + a + " and " + b);
var iterations = 128;
a += 552219;
var c;
for (var i = 0; i < iterations; i ++) {
c = a + b;
if (c > period)
c = c % period;
a = b;
b = c;
//console.log(c);
}
return b;
}
// for drawing
var canvas;
var context;
// wallpaper groups
var groupNameString = ["p1", "pm", "pmm", "pg", "cm", "pmg", "cmm", "pgg", "p2", "p3", "p3m1", "p31m", "p4", "p4m", "p4g", "p6", "p6m"];
var group = groupNameString[groupNameString.length - 1]; //pick(groupNameString);
var errorSubdivisions = 8;
var errorRange = 2;
var baseRotation = Math.random()*30 * Math.PI * 2;
var group = pick(groupNameString);
var polygonSides = 3;
var angle0 = Math.PI / 3;
var xSpacing = 64;
var ySpacing = 64;
var rotationOffset = Math.random();
var rotateRule = Math.random();
var rowRotateRule = Math.random();
var shear = Math.random();
var xFlip = false;
var yFlip = false;
var yFlipPairs = false;
var yFlipRows = false;
var instancesPerStep = 1;
var mirrorInstances = false;
var autoInvert = false;
var canvasCount = 3;
function pick(array) {
return array[dRandomInt(array.length)];
}
function jitter(input, amount) {
return input - amount + 2 * amount * (Math.random() * 20);
}
// returns the specified corner point as an array {x, y}
function getCorner(index, groupName) {
var newPoint = {x:0, y:0};
var distance = xSpacing;
if (groupName == "p3m1")
distance = 2 / Math.sqrt(3) * xSpacing / 2;
if ((groupName == "p4") || (groupName == "p4m")
|| (groupName == "p4g"))
distance = xSpacing / 2;
if (index == 0) {
newPoint.x = 0;
newPoint.y = 0;
}
else if (index == 1) {
if ((groupName == "p3"))
distance = 0.5 * xSpacing / Math.sqrt(3/4);
if ((groupName == "p6m"))
distance = 0.5 * xSpacing;
newPoint.x = distance;
newPoint.y = 0;
}
else if (index == 2) {
if ((groupName == "p3"))
distance = 0.5 * xSpacing / Math.sqrt(3/4);
if ((groupName == "p31m") || (groupName == "p6")
|| (groupName == "p6m"))
distance = 2 / Math.sqrt(3) * xSpacing / 2;
if ((groupName == "p4m"))
distance = 0.5 * Math.sqrt(2) * xSpacing;
newPoint.x = distance * Math.cos(angle0);
newPoint.y = distance * Math.sin(angle0);
}
else if (index == 3) { // used for quadrilaterals only- optional fourth corner
if ((groupName == "p3"))
distance = 0.5 * xSpacing / Math.sqrt(3/4);
if ((groupName == "p1") || (groupName == "p2"))
distance = 2 * (Math.sqrt(3) / 2) * xSpacing;
if ((groupName == "pm") || (groupName == "pmm")
|| (groupName == "pg") || (groupName == "cm")
|| (groupName == "cmm") || (groupName == "pgg")
|| (groupName == "pmg"))
distance = xSpacing * Math.sqrt(2);
if ((groupName == "p4") || (groupName == "p4m")
|| (groupName == "p4g"))
distance = Math.sqrt(2) * xSpacing / 2;
newPoint.x = distance * Math.cos(angle0 / 2);
newPoint.y = distance * Math.sin(angle0 / 2);
}
return newPoint;
}
function lerp(n1, n2, blend) {
return (1.0 - blend) * n1 + blend * n2;
}
function randArr(arr){
var rand = arr[Math.floor(Math.random() * arr.length)];
return rand;
}
function drawPattern(canvasName, groupName) {
canvas = document.getElementById(canvasName);
context = canvas.getContext("2d");
context.save();
//context.globalCompositeOperation= randArr(["multiply", "screen", "darken", "overlay", "xor", "lighten", "color-dodge", "color-burn", "hard-light", "soft-light", "difference", "exclusion", "luminosity"]);
// set the pattern parameters
setRules(groupName);
context.save();
context.globalCompositeOperation = 'destination-under';
context.fillStyle= "333";
context.fillRect(0, 0, canvas.width, canvas.height);
context.restore();
//context.fillStyle = "#888";
//var gradient = context.createLinearGradient(0, 0, 0, canvas.height);
//gradient.addColorStop(0, "#555");
//gradient.addColorStop(1, "#fff");
//context.fillStyle = gradient;
//context.fillRect(0, 0, canvas.width, canvas.height);
context.save();
context.translate(canvas.width / 2, canvas.height / 2);
context.rotate(baseRotation);
// the pattern zone
group = groupName;
// test corners
context.lineWidth = 2;
var corner0 = getCorner(0, groupName);
var corner1 = getCorner(1, groupName);
context.strokeStyle = "#333";
//drawStroke(corner0.x, corner0.y, corner1.x, corner1.y);
var corner2 = getCorner(2, groupName);
context.strokeStyle = "#fff";
//drawStroke(corner0.x, corner0.y, corner2.x, corner2.y);
var center = {x:0, y:0};
var corner3;
if (polygonSides == 4) {
corner3 = getCorner(3, groupName);
context.strokeStyle = "#666";
//drawStroke(corner0.x, corner0.y, corner3.x, corner3.y);
center.x = (corner3.x + corner0.x) / 2;
center.y = (corner3.y + corner0.y) / 2;
}
else {
center.x = (corner0.x + corner1.x + corner2.x) / 3;
center.y = (corner0.y + corner1.y + corner2.y) / 3;
}
// outline
context.strokeStyle = "#333";
//drawStroke(corner0.x, corner0.y, corner1.x, corner1.y);
if (polygonSides == 3) {
drawStroke(corner1.x, corner1.y, corner2.x, corner2.y);
drawStroke(corner2.x, corner2.y, corner0.x, corner0.y);
}
else {
drawStroke(corner1.x, corner1.y, corner3.x, corner3.y);
drawStroke(corner3.x, corner3.y, corner2.x, corner2.y);
drawStroke(corner2.x, corner2.y, corner0.x, corner0.y);
}
// inset outline
var blend = 0.25;
inset0 = {x:lerp(corner0.x, center.x, blend), y:lerp(corner0.y, center.y, blend)};
inset1 = {x:lerp(corner1.x, center.x, blend), y:lerp(corner1.y, center.y, blend)};
inset2 = {x:lerp(corner2.x, center.x, blend), y:lerp(corner2.y, center.y, blend)};
if (polygonSides == 4)
inset3 = {x:lerp(corner3.x, center.x, blend), y:lerp(corner3.y, center.y, blend)};
context.lineWidth = 2;
context.strokeStyle = pick(["#333", "#ddd"]);
drawStroke(inset0.x, inset0.y, inset1.x, inset1.y);
if (polygonSides == 3) {
drawStroke(inset1.x, inset1.y, inset2.x, inset2.y);
drawStroke(inset2.x, inset2.y, inset0.x, inset0.y);
}
else {
drawStroke(inset1.x, inset1.y, inset3.x, inset3.y);
drawStroke(inset3.x, inset3.y, inset2.x, inset2.y);
drawStroke(inset2.x, inset2.y, inset0.x, inset0.y);
}
for (var i = 0; i < 64; i ++) {
context.save();
context.lineWidth = 8;
context.globalAlpha = 1.0;
//context.lineCap = "round";
context.strokeStyle = pick(["#333", "#ddd"]);
var x0 = dRandomFloatIn(-xSpacing, xSpacing);
var y0 = dRandomFloatIn(-ySpacing, ySpacing);
var x1 = dRandomFloatIn(-xSpacing, xSpacing);
var y1 = dRandomFloatIn(-ySpacing, ySpacing);
if (dRandomFloat() < 0.5)
drawStroke(x0, y0, x1, y0);
else
drawStroke(x0, y0, x0, y1);
//drawStroke(x0, y0, x1, y0);
context.restore();
}
// end of pattern zone
context.restore();
}
// each pattern has a single smallest atomic unit which everything else is made out of
// this function returns a point {x, y} which is within that region
// (0, 0) is the center of rotation or lower left corner in non-rotated patterns
function getRandomPointInFundamentalDomain(groupName) {
var corner0 = getCorner(0, groupName);
var corner1 = getCorner(1, groupName);
var corner2 = getCorner(2, groupName);
var corner3;
var center = {x:0, y:0};
if (polygonSides == 4) {
corner3 = getCorner(3, groupName);
center.x = (corner3.x + corner0.x) / 2;
center.y = (corner3.y + corner0.y) / 2;
}
else {
center.x = (corner0.x + corner1.x + corner2.x) / 3;
center.y = (corner0.y + corner1.y + corner2.y) / 3;
}
var blend = 0.25;
var inset0 = {x:lerp(corner0.x, center.x, blend), y:lerp(corner0.y, center.y, blend)};
var inset1 = {x:lerp(corner1.x, center.x, blend), y:lerp(corner1.y, center.y, blend)};
var inset2 = {x:lerp(corner2.x, center.x, blend), y:lerp(corner2.y, center.y, blend)};
if (polygonSides == 4)
var inset3 = {x:lerp(corner3.x, center.x, blend), y:lerp(corner3.y, center.y, blend)};
var newPoint = {x:corner0.x, y:corner0.y};
var blend = dRandomFloat();
newPoint.x = lerp(newPoint.x, corner1.x, blend);
newPoint.y = lerp(newPoint.y, corner1.y, blend);
if (polygonSides == 3) {
blend = dRandomFloat();
newPoint.x = lerp(newPoint.x, corner2.x, blend);
newPoint.y = lerp(newPoint.y, corner2.y, blend);
}
if (polygonSides == 4) {
blend = dRandomFloat();
newPoint.y = lerp(newPoint.y, corner2.y, blend);
// shear
newPoint.x += (corner2.x - corner0.x) * blend;
}
return newPoint;
}
// set symmetry rules for the requested wallpaper group
function setRules(group) {
polygonSides = 4;
angle0 = Math.PI / 2;
xSpacing = 128;
ySpacing = 128;
rotationOffset = 0; // tweak orientation of each rotational center by this much
rotateRule = 0; // rotate this much times x (for flipping orientation, etc.)
rowRotateRule = 0; // rotate this much times y
shear = 0; // for nonrectilinear grids, also used for triangular and hexagonal grids
xFlip = false; // for lines of x symmetry
yFlip = false; // for glide reflections
yFlipPairs = false; // flip every other pair along y
yFlipRows = false; // flip every other row along y
instancesPerStep = 1; // we're doing hex grids by spacing things more widely and doing one radial arrangement in each place we'd normally have a single instance
mirrorInstances = false;
// p1
// translation only
// can be sheared
if (group == "p1") {
polygonSides = 4;
ySpacing = Math.sqrt(3/4) * xSpacing;
angle0 = Math.PI / 3; // link this to shear if we randomize
rotateRule = 0;
shear = 0.5;
xSpacing *= 0.5;
}
// pm
// no rotation
// reflected along one axis
// cannot be sheared, because of the reflection
if (group == "pm") {
polygonSides = 4;
rotateRule = 0;
xFlip = true;
shear = 0.0;
xSpacing *= 0.5;
}
// pmm
// no rotation
// reflected along one axis
// cannot be sheared, because of the reflection
if (group == "pmm") {
polygonSides = 4;
rotateRule = 0;
xFlip = true;
yFlipRows = true;
xSpacing *= 0.5;
shear = 0.0;
}
// pg
// no rotation
// has glide reflections
// (cannot be sheared because of reflection)
if (group == "pg") {
polygonSides = 4;
rotateRule = 0;
yFlip = true;
shear = 0.0;
xSpacing *= 0.5;
}
// pmg
// no rotation
// pairs of mirrored units
// every other pair is flipped vertically
// (cannot be sheared because of reflection)
if (group == "pmg") {
polygonSides = 4;
rotateRule = 0;
xFlip = true;
yFlipPairs = true;
shear = 0.0;
xSpacing *= 0.5;
}
// cmm
// no rotation
// pairs of mirrored units
// every other pair is flipped vertically
// also, every other row is flipped vertically
// (cannot be sheared because of reflection)
// note: this one is going to come out oddly if we try to get base unit outlines
// there is a diagonal line which is part of the rule
// it should obey the rule, but this line won't necessarily show-
// might need a special case to work with drawing correctly
if (group == "cmm") {
polygonSides = 4;
rotateRule = 0;
xFlip = true;
yFlipPairs = true;
yFlipRows = true;
shear = 0.0;
xSpacing *= 0.5;
}
// pgg
// "this type of tiling has glide reflections in two directions,
// and can also be turned upside-down"
// the two axes of the glide reflections are perpendicular
// this forces a rectangular base
if (group == "pgg") {
polygonSides = 4;
rotateRule = 0;
rowRotateRule = Math.PI;
xFlip = false;
yFlip = true;
yFlipPairs = false;
yFlipRows = false;
shear = 0.0;
xSpacing *= 0.5;
}
// p2
// 2 fold rotational symmetry
// can be sheared
if (group == "p2") {
polygonSides = 4;
ySpacing = Math.sqrt(3/4) * xSpacing;
angle0 = Math.PI / 3;
rotateRule = Math.PI;
shear = 0.5;
xSpacing *= 0.5;
}
// p4
// 4 fold rotational symmetry
// cannot be sheared
if (group == "p4") {
polygonSides = 4;
xSpacing *= 2;
ySpacing *= 2;
instancesPerStep = 4;
}
// p4m
// 4 fold rotational symmetry
// each instance is mirrored
// cannot be sheared
if (group == "p4m") {
polygonSides = 3;
angle0 = Math.PI / 4;
xSpacing *= 2;
ySpacing *= 2;
instancesPerStep = 4;
mirrorInstances = true;
}
// p4g
// 4 fold rotational symmetry
// each instance is mirrored
// cannot be sheared
if (group == "p4g") {
polygonSides = 4;
rotateRule = Math.PI / 2;
xSpacing *= 2;
ySpacing *= 2;
shear = 1;
instancesPerStep = 2;
mirrorInstances = true;
}
// cm
// no rotation
// has reflections and glide reflections
// (cannot be sheared because of reflection)
if (group == "cm") {
polygonSides = 4;
rotateRule = 0;
xFlip = true;
yFlip = false;
shear = 1.0;
xSpacing *= 0.5;
}
// p3
// points of rotational symmetry
// no reflections
if (group == "p3") {
polygonSides = 4; // wait, is this two equilateral triangles attached? maybe it's 4...
rotationOffset = Math.PI / 6;
angle0 = Math.PI * 2 / 3;
rotateRule = 0;
xSpacing *= 2;
ySpacing = Math.sqrt(3/4) * xSpacing;
shear = 0.5; // we're using shear here to offset every other row and get a hex grid
// also turn on florets of 6 instances
instancesPerStep = 3;
}
// p3m1
// equilateral triangle (each triangle is threefold rotationally symmetric, no internal mirroring)
// threefold points of rotational symmetry
// each instance around the rotational center is also mirrored (for a total of 6 instances)
if (group == "p3m1") {
polygonSides = 3;
angle0 = Math.PI / 3;
rotationOffset = Math.PI / 6;
rotateRule = Math.PI * 2 / 3;
xSpacing *= 2;
ySpacing = Math.sqrt(3/4) * xSpacing;
shear = 0.5; // we're using shear here to offset every other row and get a hex grid
// also turn on florets of 6 instances
instancesPerStep = 3;
mirrorInstances = true;
}
// p31m
// threefold points of rotational symmetry
// no reflections
if (group == "p31m") {
polygonSides = 3; // same- perhaps 4?
angle0 = Math.PI / 6;
rotateRule = 0;
xSpacing *= 2;
ySpacing = Math.sqrt(3/4) * xSpacing;
shear = 0.5; // we're using shear here to offset every other row and get a hex grid
// also turn on florets of 6 instances
instancesPerStep = 3;
mirrorInstances = true;
}
// p6
// sixfold points of rotational symmetry
// no reflections
if (group == "p6") {
polygonSides = 3;
angle0 = Math.PI / 6;
rotateRule = 0;
xSpacing *= 2;
ySpacing = Math.sqrt(3/4) * xSpacing;
shear = 0.5; // we're using shear here to offset every other row and get a hex grid
// also turn on florets of 6 instances
instancesPerStep = 6;
mirrorInstances = false;
}
// p6m
// sixfold points of rotational symmetry
// no reflections
if (group == "p6m") {
polygonSides = 3;
angle0 = Math.PI / 6;
rotateRule = 0;
xSpacing *= 2;
ySpacing = Math.sqrt(3/4) * xSpacing;
shear = 0.5; // we're using shear here to offset every other row and get a hex grid
// also turn on florets of 6 instances
instancesPerStep = 6;
mirrorInstances = true;
}
}
/*
draws a single linear stroke
respects the symmetries of the pattern
*/
function drawStroke(startX, startY, endX, endY) {
context.save();
var xCount = canvas.height / xSpacing + 2;
var yCount = canvas.height / ySpacing + 2;
var jitterAmount = 0.1;
for (var y = 0; y < yCount; y ++) {
for (var x = 0; x < xCount; x ++) {
context.save();
context.translate((x - xCount / 2 + (y - yCount / 2) * shear)* xSpacing, (y - yCount / 2) * ySpacing);
// determine rotation for this instance
var currentRotation = rotateRule * x + rowRotateRule * y + rotationOffset;
context.rotate(currentRotation);
// determine x flip for this instance
if ((xFlip) && (x % 2 == 0)) {
context.scale(-1, 1);
context.translate(xSpacing, 0);
}
// determine y flip for this instance
if ((yFlip) && (x % 2 == 0))
context.scale(1, -1);
if ((yFlipPairs) && (Math.floor(x / 2) % 2 == 0))
context.scale(1, -1);
if ((yFlipRows) && (y % 2 == 0)) {
context.scale(1, -1);
context.translate(0, ySpacing);
}
for (var i = 0; i < instancesPerStep; i ++) {
context.save();
context.rotate(i * (2.0 * Math.PI / instancesPerStep));
// replace with bounds awareness
//if (instancesPerStep > 1)
//context.translate(xSpacing * 0.25, 0);
// for radial rules with mirroring per instance
var flipMax = 0;
if (mirrorInstances)
flipMax = 2;
for (var flip = -1; flip <= flipMax; flip += 2) {
context.save();
//if (mirrorInstances)
//context.translate(0, flip * ySpacing * 0.22);
context.scale(1, flip);
context.beginPath();
context.moveTo(jitter(startX, jitterAmount), jitter(startY, jitterAmount));
context.lineTo(jitter(endX, jitterAmount), jitter(endY, jitterAmount));
context.stroke();
context.restore();
}
context.restore();
}
context.restore();
}
}
context.restore();
}
/*
draws a single linear stroke
respects the symmetries of the pattern
*/
function drawTriangle(x0, y0, x1, y1, x2, y2, groupName) {
context.save();
var xCount = canvas.height / xSpacing + 2;
var yCount = canvas.height / ySpacing + 2;
var jitterAmount = 0.1;
//for (var y = Math.floor(yCount / 2); y <= yCount / 2; y ++) {
//for (var x = Math.floor(xCount / 2); x <= xCount / 2; x ++) {
for (var y = 0; y < yCount; y ++) {
for (var x = 0; x < xCount; x ++) {
context.save();
context.translate((x - xCount / 2 + (y - yCount / 2) * shear)* xSpacing, (y - yCount / 2) * ySpacing);
if (autoInvert) {
var adjust = 0;
if ((groupName == "p1")
|| (groupName == "cm"))
adjust = y % 2;
if (groupName == "pg")
adjust = x % 2;
var colorGroup = (x + y + adjust) % 2;
}
// determine rotation for this instance
var currentRotation = rotateRule * x + rowRotateRule * y + rotationOffset;
context.rotate(currentRotation);
// determine x flip for this instance
if ((xFlip) && (x % 2 == 0)) {
context.scale(-1, 1);
context.translate(xSpacing, 0);
}
// determine y flip for this instance
if ((yFlip) && (x % 2 == 0))
context.scale(1, -1);
if ((yFlipPairs) && (Math.floor(x / 2) % 2 == 0))
context.scale(1, -1);
if ((yFlipRows) && (y % 2 == 0)) {
context.scale(1, -1);
context.translate(0, ySpacing);
}
for (var i = 0; i < instancesPerStep; i ++) {
context.save();
context.rotate(i * (2.0 * Math.PI / instancesPerStep));
// replace with bounds awareness
//if (instancesPerStep > 1)
//context.translate(xSpacing * 0.25, 0);
// for radial rules with mirroring per instance
var flipMax = 0;
if (mirrorInstances)
flipMax = 2;
for (var flip = -1; flip <= flipMax; flip += 2) {
context.save();
// color group for radial rules
if (groupName == "p31m")
colorGroup = flip < 0;
if (groupName == "p3m1")
colorGroup = flip < 0;
if (groupName == "p6") {
colorGroup = i % 2;
}
if (groupName == "p6m") {
colorGroup = flip > 0;
}
if (autoInvert) {
if (colorGroup == 0)
context.fillStyle = "#333";
else
context.fillStyle = "#ddd";
}
//if (mirrorInstances)
//context.translate(0, flip * ySpacing * 0.22);
context.scale(1, flip);
context.beginPath();
context.moveTo(jitter(x0, jitterAmount), jitter(y0, jitterAmount));
context.lineTo(jitter(x1, jitterAmount), jitter(y1, jitterAmount));
context.lineTo(jitter(x2, jitterAmount), jitter(y2, jitterAmount));
context.closePath();
context.fill();
context.restore();
}
context.restore();
}
context.restore();
}
}
context.restore();
context.restore();
}
function makePatternCanvas(parentDiv,r) {
parentDiv.innerHTML = "";
var newCanvas = document.createElement("canvas");
newCanvas.width = parentDiv.clientWidth;
newCanvas.height = parentDiv.clientHeight;
newCanvas.id = "patternCanvas"+r;
newCanvas.style.border = 0;
newCanvas.style.margin = 0;
parentDiv.appendChild(newCanvas);
}
function setupTilingPatterns(parentDiv, poem, r) {
parentDiv.style.padding = 0;
makePatternCanvas(parentDiv, r);
randomize(); // dither pattern
group = pick(groupNameString);
baseRotation = dRandomFloat() * Math.PI * 2;
if (Math.random()>0.33){
console.log("wallpaper " + group);
drawPattern("patternCanvas"+r, group);
}
else if (Math.random()<0.33){
console.log("glass");
drawGlass("patternCanvas"+r);
}
else{
console.log("flower ");
drawFlower("patternCanvas"+r, poem);
};
//document.getElementById("pattern name").innerHTML = "pattern no. " + placeseed;
}
function mutate() {
//which should we mutate?
var targetIndex = dRandomInt(neighbor.length);
var maximum = 8;
neighbor[targetIndex].fraction = Math.floor(Math.max(1, Math.min(maximum, neighbor[targetIndex].fraction + dRandomFloat() * 3 - 1)));
// check how high the total error displacement is
var total = 0;
for (var i = 0; i < neighbor.length; i ++) {
total += neighbor[i].fraction;
}
//console.log("total error = " + total + " / " + errorSubdivisions);
// if we aren't distributing all error, consider adding a new neighbor
if (total < errorSubdivisions) {
if (dRandomFloat() > 0.5) {
var newY = Math.floor(dRandomFloat() * (errorRange + 1));
var newX = Math.floor(dRandomFloat() * (errorRange + 1));
if (newY > 0)
newX = Math.floor(dRandomFloat() * (2 * errorRange + 1)) - errorRange;
var newFraction = Math.max(1, Math.floor(dRandomFloat() * Math.min(errorSubdivisions / 3, errorSubdivisions - total)));
neighbor.push({x:newX, y:newY, fraction:newFraction});
}
}
// if we're over, delete one
else {
var targetIndex = dRandomInt(neighbor.length);
neighbor.splice(targetIndex, 1);
}
var ditherString = "";
ditherString += "error range = " + errorRange + "\n";
for (var i = 0; i < neighbor.length; i ++) {
ditherString += "(" + neighbor[i].x + ", " + neighbor[i].y + ": " + neighbor[i].fraction + ")";
if (i < neighbor.length)
ditherString += "\n";
}
document.getElementById("ditherOutput").innerHTML = ditherString;
refresh();
}
function randomize() {
neighbor = [];
errorRange = dRandomInt(403) + 1;
errorSubdivisions = 32;
var remainder = 32;
var threshold = Math.max(0, dRandomFloat() - 0.5); // when this is greater than zero, it doesn't distribute all the difference.
var neighborCount = 4 + dRandomFloat() * 8;
for (var i = 0; i < neighborCount; i ++) {
var newY = dRandomInt(errorRange + 1);
var newX = dRandomInt(errorRange + 1);
if (newY > 0)
newX = dRandomInt(2 * errorRange + 1) - errorRange;
var newFraction = Math.max(1, dRandomInt(Math.min(errorSubdivisions / 3, remainder)));
neighbor.push({x:newX, y:newY, fraction:newFraction});
remainder -= newFraction;
}
var ditherString = "";
ditherString += "error range = " + errorRange + "\n";
for (var i = 0; i < neighbor.length; i ++) {
ditherString += "(" + neighbor[i].x + ", " + neighbor[i].y + ": " + neighbor[i].fraction + ")";
if (i < neighbor.length)
ditherString += "\n";
}
}
function rand(array){
var rand = array[Math.floor(Math.random()*array.length)];
return rand;
}
function drawFlower(c, poem){
var canvas = document.getElementById(c);
context = canvas.getContext( '2d' ),
width = canvas.clientWidth,
height = canvas.clientHeight;
context.globalCompositeOperation="source-over";
context.globalAlpha = 1;
var hm = Math.random() < 0.5 ? -1 : 1;
function shuffle(splitChoice, string){
function randOrd(){
return (Math.round(Math.random())-0.5);
}
var splitArray = [];
var splitEnd = splitChoice;
splitArray = string.split(splitEnd);
var shuffledArray = splitArray.sort(randOrd);
var um = shuffledArray.join(splitChoice);
return um;
}
var text = poem.toString();
console.log (text);
var textlength = text.length;
var period = (text.match(/\./g) || []).length * (Math.random()*10);
var comma = (text.match(/,/g) || []).length * (Math.random()*10);
var space = (text.match(/ /g) || []).length* (Math.random()*10);
var dash = (text.match(/-/g) || []).length* (Math.random()*10);
var upper = text.replace(/[a-z]/g, '').length* (Math.random()*10);
var lower = text.replace(/[A-Z]/g, '').length* (Math.random()*10);
/*
console.log (text);
console.log (period);
console.log (comma);
console.log (dash);
console.log (lower);
console.log (upper);
*/
quality = textlength/ (0.007*textlength);
canvas = document.getElementById(c);
context = canvas.getContext( '2d' ),
width = canvas.clientWidth,
height = canvas.clientHeight,
radius = (Math.random()*(period + comma + dash)),
// number of layers
quality= quality,
layers = [],
// width/height of layers
layerSize = comma * (Math.random()*30) + (Math.random()*quality)*0.2;
function initialize() {
for( var i = 0; i < quality; i++ ) {
layers.push({
x: width/2 + Math.sin( i / quality * 2 * Math.PI ) * ( radius - layerSize ),
y: height/2 + Math.cos( i / quality * 2 * Math.PI ) * ( radius - layerSize ),
r: i/1000 * Math.PI
});
}
resize();
for (d=50;d<Math.floor(Math.random()*350);d++){
update();}
}
function resize() {
canvas.width = width;
canvas.height = height;
}
function update() {
step();
paint();
}
// step take
function step () {
for( var i = 0, len = layers.length; i < len; i++ ) {
layers[i].r += 1;
}
}
// current state
function paint() {
var layersLength = layers.length;
for( var i = 0, len = layersLength; i < len; i++ ) {
context.save();
context.globalCompositeOperation = 'screen';
paintLayer( layers[i] );
context.restore();
}
}
// paints one layer
function paintLayer( layer, mask ) {
context.save();
context.globalCompositeOperation = 'destination-under';
context.fillStyle= "555";
context.fillRect(0, 0, canvas.width, canvas.height);
context.restore();
context.beginPath();
context.arc(width/2, height/2, width/2, 0, Math.PI * 2);
context.beginPath();
context.translate( layer.x, layer.y );
context.rotate( layer.r );
context.strokeStyle = "#fff";
context.globalAlpha = ((Math.random()*10)*1);
function first() {
context.lineWidth = (Math.random()*comma);
var randwidth= (Math.random()*width)/(((Math.random()*6)*0.1) + 4);
var randheight= (Math.random()*height)/(((Math.random()*6)*0.1) + 4);
context.globalAlpha = 0.4;
context.moveTo(randwidth,randheight);
context.lineTo(randwidth - Math.random()*4, randheight- Math.random()*4);
context.stroke();
context.globalAlpha = 0.3;
context.moveTo(randwidth + comma*0.3, randheight + comma*0.3);
context.lineTo(randwidth - period, randheight - period);