-
Notifications
You must be signed in to change notification settings - Fork 17
/
test.html
174 lines (147 loc) · 4.72 KB
/
test.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<html>
<body>
<div style="display:flex;justify-content:center;">
<canvas
id="canvas"
style="border: 1px solid black;"
></canvas>
</div>
<hr/>
<button id="newRandBtn">New Random</button>
<br/>
<br/>
<textarea id="input">e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855</textarea>
<button id="fromBtn">From String</button>
<br/>
<a href="https://emn178.github.io/online-tools/sha3_256.html" target="_blank">SHA3 online tool</a>
<script>
const randBtn = document.getElementById('newRandBtn')
const input = document.getElementById('input')
const fromStrBtn = document.getElementById('fromBtn')
const canvas = document.getElementById('canvas')
const canvasSize = 250
const colorPallete = [
'#f94144',
'#f3722c',
'#f8961e',
'#f9c74f',
'#90be6d',
'#43aa8b',
]
const bgColor = '#577590'
function drawTriangle(ctx, p1, p2, p3) {
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.lineTo(p3.x, p3.y);
ctx.fill();
}
function drawEquilateralTriangle(ctx, x, y, length, angle) {
// console.log(x, y, length, angle)
// degrees to radians
const radian = Math.PI/180
angle = Math.abs(angle)
if (Number.isNaN(angle)) angle = 0
const p2 = {
x: x + Math.cos((angle + 150)*radian) * length,
y: y + Math.sin((angle + 150)*radian) * length,
}
const p3 = {
x: x + Math.cos((angle + 210)*radian) * length,
y: y + Math.sin((angle + 210)*radian) * length,
}
drawTriangle(ctx, { x, y }, p2, p3)
}
function cropCircle() {
const ctx = canvas.getContext('2d')
const halfHeight = canvas.height/2
const halfWidth = canvas.width/2
const img = ctx.toDataURL()
ctx.beginPath()
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.closePath()
ctx.fill()
ctx.drawImage(img,0,0);
// ctx.globalCompositeOperation='destination-in'
ctx.beginPath()
ctx.arc(halfWidth,halfHeight,halfHeight,0,Math.PI*2)
ctx.closePath()
ctx.fill()
}
function randomImage() {
canvas.width = canvasSize
canvas.height = canvasSize
const ctx = canvas.getContext('2d');
const halfHeight = canvas.height/2
const halfWidth = canvas.width/2
ctx.fillStyle = bgColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
const sideLength = halfHeight/2
for (var i = 0; i < colorPallete.length; i++) {
ctx.fillStyle = colorPallete[i % colorPallete.length];
const quadrant = (i % 4) + 1
const up = quadrant === 1 || quadrant === 2
const left = quadrant === 2 || quadrant === 3
const xOffset = Math.random() * sideLength * (left ? -1: 1)
const yOffset = Math.random() * sideLength * (up ? -1: 1)
let startAngle = 0
if(quadrant === 4) startAngle = 0
if(quadrant === 3) startAngle = 90
if(quadrant === 2) startAngle = 180
if(quadrant === 1) startAngle = 270
const angle = startAngle + (Math.random() * 90)
// console.log(quadrant, up, left, startAngle, angle)
drawEquilateralTriangle(
ctx,
xOffset + halfWidth,
yOffset + halfHeight,
halfWidth*2,
angle,
)
}
}
function fromString(string='') {
if (!/^[0-9a-f]{64}$/i.test(string)) return alert('Must be hex string with 64 characters')
console.log(string)
const ctx = canvas.getContext('2d');
const halfHeight = canvas.height/2
const halfWidth = canvas.width/2
const sideLength = halfHeight/2
ctx.fillStyle = bgColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
let quadrant = 1
for (var i = 0; i < string.length/8; i++) {
ctx.fillStyle = colorPallete[i % colorPallete.length];
const up = quadrant === 1 || quadrant === 2
const left = quadrant === 2 || quadrant === 3
const x = parseInt(string.substring(i*8 + 0, i*8 + 2), 16) / 256
const y = parseInt(string.substring(i*8 + 6, i*8 + 8), 16) / 256
const xOffset = x * sideLength * (left ? -1: 1)
const yOffset = y * sideLength * (up ? -1: 1)
let startAngle = 0
if(quadrant === 4) startAngle = 0
if(quadrant === 3) startAngle = 90
if(quadrant === 2) startAngle = 180
if(quadrant === 1) startAngle = 270
const angle = startAngle + (parseInt(string.substring(i*8, (i+1)*8), 16) % 90)
// console.log(x, y)
// console.log(quadrant, up, left, startAngle, angle)
drawEquilateralTriangle(
ctx,
xOffset + halfWidth,
yOffset + halfHeight,
halfWidth*2,
angle,
)
quadrant = (quadrant % 4) + 1
}
}
randBtn.addEventListener('click', randomImage)
fromStrBtn.addEventListener('click', function() {
fromString(input.value)
cropCircle()
})
randomImage()
</script>
</body>
</html>