-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
323 lines (303 loc) · 11.4 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
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
<html>
<head>
<style>
@media screen {
#images {
border-bottom:2px solid black;
padding-bottom:5px;
margin-bottom:5px;
}
#template canvas {
width:100%;
position:absolute;
}
.sourceWrapper {
width:200px;
height:203px;
position:relative;
display: inline-block;
margin: 2px;
font-size:0px
}
.source {
height:173px;
}
.imageOption {
height:30px;
width:20%;
vertical-align: bottom;
font-size:13px;
}
.stretchMode{
padding:0px;
}
.stretchMode>div {
width:100%;
height:100%;
background-image:url('stretchIcon.png');
background-repeat: no-repeat;
background-repeat: no-repeat;
background-position-x: 4px;
background-position-y: -3px;
}
.showOverlays .overlay {
position: absolute;
background: url('guide.png');
background-size: 100% 100%;
width: 100%;
height: 100%;
}
#options {
display: inline-block;
width:4em;
vertical-align:top;
}
#options>*{
width:100%;
height:4em;
}
}
@media print {
@page {size: landscape}
html,body{margin:0px;width:1020px}
#images,#options {
display:none;
}
#template {
max-width:100%;
max-height:100%;
}
#template canvas {
max-width:100%;
max-height:100%;
position:absolute;
}
}
</style>
<script src="jQuery.js" ></script>
<script>
var triangles;
var r = 400;
var stillGenerating = true; //flag to prevent the URL being updated whilst we are still initally reading it
$(document).ready(function(){
/*
* The Template for creatin the source inputs from
* This is taken from the DOM, has it's event hooks applied
* then is stored until cloned.
*/
sourceTemplate = $(".sourceWrapper").detach()
//Hook the edit button's click so we can edit the src.
sourceTemplate.find(".edit").click(function(){
img = $(this).parent().find("img")
input = window.prompt("New image URL",img.attr("src"))
if (input != ""){
img.attr("src",input);
}
})
//Hook the img's load so we know when we can draw it onto the canvas
sourceTemplate.find(".source img").load(function(){
ctx = this.parentNode.getContext("2d");
var xScale = this.parentNode.width/this.width
var yScale = this.parentNode.height/this.height
var scale;
if ($(this).parents(".sourceWrapper").find(".stretchMode").attr("data-status") == 0){
scale = Math.min(xScale,yScale)
} else {
scale = Math.max(xScale,yScale)
}
ctx.fillStyle = $(this).parents(".sourceWrapper").find(".bgColor").val()
ctx.fillRect (0,0,this.parentNode.width,this.parentNode.height);
ctx.drawImage(this,this.parentNode.width/2-this.width*scale/2,this.parentNode.height/2-this.height*scale/2,this.width*scale,this.height*scale);
buildTemplate();
updateURL();
})
//Hook the bg color selector chaning so we know when to update the canvas
sourceTemplate.find(".bgColor").change(function(){
$(this).parent().find("img").load()
})
//Hook the stretch mode cick so we know when to update the canvas and toggle the button.
sourceTemplate.find(".stretchMode").click(function(){
var state = ($(this).attr("data-status")*1+1)%2;
console.log(state);
$(this).attr("data-status",state).find("div").css("background-position-y",(-3-24*state)+"px");
$(this).parent().find("img").load()
})
/* Anayalse the hash part of the URL to determin what to use for the settings
* The hash part is used becuase as the user interacts with and changes the
* hexaflexagon it an be updated without the browser redirecting them
* The inital '#' is discarded then the string is split by '/'s and then again
* by '&' to give data about each image source.
* The data (in order) is URL, bgColor, stretchMode.
*/
data = document.location.hash.substr(1).split("/")
console.log(data)
defColors = ["#fdbc5e","#4c3da2","#71c1e6","#f88eb2","#f9ef7e","#caaad1"]
for(i=0;i<=5;i++){
var im = addImage();
var info;
if( i<data.length){
info = data[i].split("&")
} else {
info=[];
}
if(info.length>0 && info[0]){
im.find("img").attr("src",decodeURIComponent(info[0]));
} else {
im.find("img").attr("src","mark"+i+".png");
}
if(info.length>1 && info[1]){
console.log(decodeURIComponent(info[1]))
im.find(".bgColor").val(decodeURIComponent(info[1]));
} else {
im.find(".bgColor").val(defColors[i]);
}
if(info.length>2 && info[2]){
im.find(".stretchMode").attr("data-status",decodeURIComponent(info[2]));
}
}
})
/* Adds a clone of the sourceTemplate, the wrapper for image inputing,
* to the image source area.
*/
function addImage(){
return sourceTemplate.clone(true).appendTo($("#images"));
}
/* Updates the has part of the URL after one of the options has changed
* The data is seperated by slashes (/) and is orderd with the background
* colour first followed by the URL of the source images in order.
*/
function updateURL(){
var h = ""
$(".sourceWrapper").each(function(){
h += encodeURIComponent($(this).find("img").attr("src"))+"&"
+ encodeURIComponent($(this).find(".bgColor").val())+"&"
+ encodeURIComponent($(this).find(".stretchMode").attr("data-status"))+"/"
})
document.location.hash = h
}
/* List of hexaflexagon templates */
//TODO explain how the template format works
templates = {
"basic":{
0 :[ [0,5,1],[2,5,2],[1,5,1],[0,4,2],[2,4,1],[1,4,2],[0,3,1],[2,3,2],[1,3,1],[-1,"A",0]],
1 :[[-1,"X",0],[5,5,3],[5,4,2],[3,5,3],[3,4,2],[4,5,3],[4,4,2],[5,3,3],[5,2,2],[3,3,3] ],
2.35:[ [0,2,2],[2,2,1],[1,2,2],[0,1,1],[2,1,2],[1,1,1],[0,0,2],[2,0,1],[1,0,2],[-1,"Y",1]],
3.35:[[-1,"B",1],[3,2,2],[4,3,3],[4,2,2],[5,1,3],[5,0,2],[3,1,3],[3,0,2],[4,1,3],[4,0,2] ]
}
}
/* Generates the hexaflexagon template */
function buildTemplate(){
/* Create refrences to the canvases, the source images and the template we will use
* At current the tempate is locked to basic. The plan is to offer more allowing for
* non-hexahexaflexagon hexaflexagons
*/
var ups = $("#ups")[0];
var down = $("#downs")[0];
var sources = $("#images canvas");
template = templates["basic"]
//Determin the height of the template by finding the largest key, adding 1 and multiplying by r
var h=0;
for(var i in template){
if(i>h){h=i*1}
}
//Resize both canvases so the template will fit
ups.width = (template[0].length+1)*r/2
ups.height = (h+1)*r*Math.sqrt(3)/2
downs.width = ups.width
downs.height = ups.height
//Create refrences to the canvas' contexts
var ctxUp = ups.getContext("2d");
var ctxDown = downs.getContext("2d");
//Iterate over the template drawing it as we go
for(var i in template){
for(var j in template[i]){
if (template[i][j].length == 3){
drawTriangle(template[i][j][0],template[i][j][1],j/2,i*1,template[i][j][2]);
}
}
}
/* This function does the drawing
* ( It could be refrenced in the loop but for historical reason it isn't
* It may be moved there eventally )
* The function draws the given source's segment at the given triangle grid positions
* with the given rotation.
* ( Each triangle going horizontally is +0.5x the distance between 2 adjacent
* triangles of the same oriantation along the x axis is thefore 1 )
* ( Rotation is from 0-5 (inclusive) and is the number of 1 6ths of a turn the triangle
* has undergone to get from it's inital position in the hexagon to it's current )
* Source (src) can also be -1 which Insted of drawing a hexagon segment will write the
* text provided in seg in the center of the triangle, allowing for glue flaps to be
* easily labled
*/
//TODO draw onto offscreen canvas first then crop before pasting into screen
function drawTriangle(src,seg,x,y,ro) {
/* Identify weather the triangle we are going to draw points up or down
* Allowing us to select the canvas to draw it on to, position it
* correctlly (Triangles pointing down have their centers of rotation
* slightly higher than that of up pointing trinagles) and draw the clipping
* gluide for it
*/
if((ro)%2 == 1){ //up pointing triangle
ctx = ctxUp
ctx.save()
ctx.fillStyle = "#999";
ctx.globalCompositeOperation = "source-over"
ctx.beginPath()
ctx.translate((x+0.5)*r,(r*Math.sqrt(3)/2)*(y+2/3));
ctx.moveTo( 0,-Math.sqrt(3)/3*r);
ctx.lineTo(-r/2, Math.sqrt(3)/6*r);
ctx.lineTo( r/2, Math.sqrt(3)/6*r);
} else { //down pointing triangle
ctx = ctxDown
ctx.save()
ctx.fillStyle = "#999";
ctx.globalCompositeOperation = "source-over"
ctx.beginPath()
ctx.translate((x+0.5)*r,(r*Math.sqrt(3)/2)*(y+1/3));
ctx.moveTo( 0, Math.sqrt(3)/3*r);
ctx.lineTo(-r/2,-Math.sqrt(3)/6*r);
ctx.lineTo( r/2,-Math.sqrt(3)/6*r);
}
ctx.fill();
// Draw either the segment of the hexagon
// Or the text is src = -1
if(src >= 0){
ctx.globalCompositeOperation = "source-atop";
ro-=seg;
ctx.rotate(ro*Math.PI/3)
var sw = sources[src].width/2
var sh = sources[src].height/2
var sx = [sw/2,sw,sw,sw/2,0,0];
var sy = [0,0,sh,sh,sh,0];
ctx.drawImage(sources[src],sx[seg],sy[seg],sw,sh,-r/2,((seg%2)+1)/3*(-Math.sqrt(3)/2*r),r,(Math.sqrt(3)/2*r))
} else if (src==-1) {
ctx.font = "Bold "+(r/3)+"px Arial"
ctx.fillStyle = "#000";
ctx.textAlign = "center";
ctx.fillText(seg,0,50)
}
ctx.restore()
}
}
</script>
</head>
<body>
<div id="images">
<div class="sourceWrapper">
<div class="overlay"></div>
<canvas class="source" width="800px" height="693px">
<img>
</canvas>
<button class="imageOption moveLeft"><<<</button>
<input class="imageOption bgColor" type="color"/>
<button class="imageOption edit">Edit</button>
<button class="imageOption stretchMode" data-status="0"><div></div></button>
<button class="imageOption moveRight">>>></button>
</div>
</div>
<div id="template">
<canvas id="downs" width="0px" height="0px" ></canvas>
<canvas id="ups" width="0px" height="0px" ></canvas>
</div>
</body>
</html>