-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
147 lines (129 loc) · 4.41 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Canvas Test</title>
</head>
<body>
<canvas id="c" height="400px" width="375"></canvas>
<div id="sliders">
</div>
<button onclick="randomize()">Randomize</button>
</div>
</body>
<style>
html { background-color: black; }
body {
margin: 0 auto;
width: 375px;
height: 800px;
background-color: grey;
}
canvas { background-color: white; }
button { font-size: 30px; width: 100%;}
div#sliders > label { font-size: 20px; display: inline-block; width: 25%;}
div#sliders > input { display: inline-block; width: 70%;}
</style>
<script>
const canvas = document.getElementById("c")
const ctx = canvas.getContext('2d')
let body = {
height: {value: 175, min: 80, max: 220},
width: {value: 75, min: 20, max: 150},
head: {value: 25, min: 20, max: 40},
neck: {value: 15, min: 8, max: 23},
shoulder: {value: 0, min: -50, max: 50},
chest: {value: 0, min: -50, max: 50},
waist: {value: 0, min: -50, max: 50},
hip: {value: 0, min: -50,max: 50},
foot: {value: 0, min: -50, max: 50},
}
function init(){
renderBodyView()
renderUIView()
}
function randomize(){
randomizeBody()
renderBodyView()
renderUIView()
}
function sliderController(){
updateBodyFromSliders()
renderBodyView()
}
function randomizeBody(){
Object.keys(body).forEach(function(key){
body[key].value = randRange(body[key].min, body[key].max)
})
}
function updateBodyFromSliders(){
Object.keys(body).forEach(function(key){
body[key].value = Number(document.getElementById(key).value)
})
}
function generateCoordsFromBody(){
const center = canvas.width / 2
const base = canvas.height - 40
const coords = {
head: {x: center, y: base - body.height.value - body.head.value - body.neck.value},
lShoulder: { x: center - body.width.value - body.shoulder.value, y: base - body.height.value },
rShoulder: { x: center + body.width.value + body.shoulder.value, y: base - body.height.value },
lChest: { x: center - body.width.value - body.chest.value, y: base - body.height.value * .85 },
rChest: { x: center + body.width.value + body.chest.value, y: base - body.height.value * .85},
lWaist: { x: center - body.width.value - body.waist.value, y: base - body.height.value * .6 },
rWaist: { x: center + body.width.value + body.waist.value, y: base - body.height.value * .6 },
lHips: { x: center - body.width.value - body.hip.value, y: base - body.height.value * .45 },
rHips: { x: center + body.width.value + body.hip.value, y: base - body.height.value * .45 },
lFoot: { x: center - body.width.value - body.foot.value, y: base },
rFoot: { x: center + body.width.value + body.foot.value, y: base }
}
return coords
}
function renderBodyView(){
clearCanvas()
const coords = generateCoordsFromBody()
ctx.beginPath();
ctx.moveTo(coords.lShoulder.x, coords.lShoulder.y);
ctx.lineTo(coords.lChest.x, coords.lChest.y);
ctx.lineTo(coords.lWaist.x, coords.lWaist.y);
ctx.lineTo(coords.lHips.x, coords.lHips.y);
ctx.lineTo(coords.lFoot.x, coords.lFoot.y);
ctx.lineTo(coords.rFoot.x, coords.rFoot.y);
ctx.lineTo(coords.rHips.x, coords.rHips.y);
ctx.lineTo(coords.rWaist.x, coords.rWaist.y);
ctx.lineTo(coords.rChest.x, coords.rChest.y);
ctx.lineTo(coords.rShoulder.x, coords.rShoulder.y);
ctx.closePath();
ctx.stroke();
ctx.beginPath()
ctx.arc(coords.head.x, coords.head.y, body.head.value, 0, Math.PI * 2)
ctx.closePath()
ctx.stroke()
}
function renderUIView(){
sliderDiv = document.getElementById("sliders")
sliderDiv.innerHTML= ''
Object.keys(body).forEach(function(key){
let label = document.createElement('label')
label.innerHTML = key
sliderDiv.appendChild(label)
let slider = document.createElement('input')
slider.id = key
slider.type = "range"
slider.min = body[key].min
slider.max = body[key].max
slider.value = body[key].value
slider.setAttribute("oninput", "sliderController()")
sliderDiv.appendChild(slider)
})
}
function randRange(min, max) {
return Math.random() * (max - min) + min;
}
function clearCanvas(){
ctx.clearRect(0, 0, canvas.width, canvas.height)
}
init()
</script>
</html>