-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtobeimplemented-dialog.html
357 lines (280 loc) · 9.25 KB
/
tobeimplemented-dialog.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
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
<!DOCTYPE HTML>
<html>
<head>
<meta charset=utf-8>
<title>Freehand</title>
<style>
html, body, #drawing {
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
}
#menu_o {
background-color:black;
width: 100%;
}
</style>
<script type="text/javascript" src="svg.js" ></script>
<script type="text/javascript" src="svg.draggable.js" ></script>
<script type="text/javascript" src="require.js" ></script>
</head>
<body>
<div id="drawing"></div>
<div id="menu_o"></div>
<script type="text/javascript">
var svg = SVG("drawing")
var diag = svg.group()
var dialog = {
source: [100,300],
center: [500, 250],
corner: [700, 50],
text: "A module is different from a traditional script file in that it defines a well-scoped object that avoids polluting the global namespace. It can explicitly list its dependencies and get a handle on those dependencies without needing to refer to global objects, but instead receive the dependencies as arguments to the function that defines the module."
}
var box={}, font1
var module = { exports: {}}
var defined //= require('defined');
var wordWrapper //= require('word-wrapper');
var assign //= require('object-assign');
var computeLayout, opentype
var fontSizePx = 26;
require(['node_modules/defined/index.js'], function(){
defined = module.exports
})
require(['node_modules/word-wrapper/index.js'], function(){
wordWrapper = module.exports
})
require(['node_modules/object-assign/index.js'], function(){
assign = module.exports
})
require(['node_modules/opentype-layout/index.js'], function(){
console.log(module)
computeLayout = module.exports
console.log(defined, assign, wordWrapper, computeLayout)
})
require([
//'abs-svg-path',
'node_modules/opentype.js/dist/opentype.js'
], function(
//absolutize,
o
){
opentype = module.exports
console.log(o)
//computeLayout = module.exports.layout
o.load('FredokaOne-Regular.ttf', function (err, font) {
if (err) throw err;
font1 = font
});
});
function draw_controls(dialog){
var source = svg.circle(15).cx(dialog.source[0]).cy(dialog.source[1])
var center = svg.circle(15).cx(dialog.center[0]).cy(dialog.center[1])
var corner = svg.circle(15).cx(dialog.corner[0]).cy(dialog.corner[1])
source.draggable().on('dragmove', function(e){
//e.preventDefault()
//this.move(e.detail.p.x, e.detail.p.y)
dialog.source[0]= e.detail.p.x
dialog.source[1]= e.detail.p.y
//draw_bubble(dialog)
mdraw_text(dialog)
})
center.draggable().on('dragmove', function(e){
//e.preventDefault()
//this.move(e.detail.p.x, e.detail.p.y)
dialog.center[0]= e.detail.p.x
dialog.center[1]= e.detail.p.y
//draw_bubble(dialog)
mdraw_text(dialog)
})
corner.draggable().on('dragmove', function(e){
//e.preventDefault()
//this.move(e.detail.p.x, e.detail.p.y)
dialog.corner[0]= e.detail.p.x
dialog.corner[1]= e.detail.p.y
mdraw_text(dialog)
})
}
function mdraw_text(dialog){
draw_bubble(dialog)
var ndx = 0
var prev = [300]
var dg = draw_text(dialog)
while(!dg){
ndx = ndx + 1
console.log(fontSizePx)
if (prev[0] == fontSizePx) return true
prev[ndx%2] = fontSizePx
dg = draw_text(dialog)
}
}
function draw_bubble(dialog){
if (diag) diag.clear()
var corner_rate = 0.20, c_rate=1.5
var cen = dialog.center
var src = dialog.source
var loc = "", index
//var box = []
var patharray=[]
var choices = [3,4,8,9,13,14,18,19]
var max = [Math.abs(dialog.center[0]-dialog.corner[0]),Math.abs(dialog.center[1]-dialog.corner[1])]
var corner_diff= Math.min(max[0], max[1])*corner_rate, hcrn=corner_diff/2
box = {
width: 2*(max[0]-hcrn),
height: 2*(max[1]-hcrn),
x1: cen[0]-max[0]+hcrn,
y1: cen[1]-max[1]+hcrn,
x2: cen[0]+max[0],
y2: cen[1]+max[1]
}
//var rect = svg.rect(2*max[0], 2*max[1]).cx(cen[0]).cy(cen[1]).radius(corner_diff, corner_diff)
//rect.attr({fill: '#f06',opacity:0.1})
//diag.add(rect)
patharray=[
["M", cen[0]-max[0], cen[1]-max[1]+corner_diff],
["C", cen[0]-max[0], cen[1]-max[1]+hcrn, cen[0]-max[0]+hcrn, cen[1]-max[1], cen[0]-max[0]+corner_diff, cen[1]-max[1]],
["L", cen[0]-c_rate*corner_diff, cen[1]-max[1]],
["L", cen[0], cen[1]-max[1]], // 3
["L", cen[0]+c_rate*corner_diff, cen[1]-max[1]], // 4
["L", cen[0]+max[0]-corner_diff, cen[1]-max[1]],
["C", cen[0]+max[0]-hcrn, cen[1]-max[1], cen[0]+max[0], cen[1]-max[1]+hcrn, cen[0]+max[0], cen[1]-max[1]+corner_diff],
["L", cen[0]+max[0], cen[1]-c_rate*corner_diff],
["L", cen[0]+max[0], cen[1]], // 8
["L", cen[0]+max[0], cen[1]+c_rate*corner_diff], // 9
["L", cen[0]+max[0], cen[1]+max[1]-corner_diff],
["C", cen[0]+max[0], cen[1]+max[1]-hcrn, cen[0]+max[0]-hcrn, cen[1]+max[1], cen[0]+max[0]-corner_diff, cen[1]+max[1]],
["L", cen[0]+c_rate*corner_diff, cen[1]+max[1]],
["L", cen[0], cen[1]+max[1]], // 13
["L", cen[0]-c_rate*corner_diff, cen[1]+max[1]], // 14
["L", cen[0]-max[0]+corner_diff, cen[1]+max[1]],
["C", cen[0]-max[0]+hcrn, cen[1]+max[1], cen[0]-max[0], cen[1]+max[1]-hcrn, cen[0]-max[0], cen[1]+max[1]-corner_diff],
["L", cen[0]-max[0], cen[1]+c_rate*corner_diff],
["L", cen[0]-max[0], cen[1]], // 18
["L", cen[0]-max[0], cen[1]-c_rate*corner_diff], // 19
["L", cen[0]-max[0], cen[1]-max[1]+corner_diff],
["Z"]
]
if (src[0]-cen[0] < 0) {
loc = "0"
} else {
loc = "1"
}
if (src[1]-cen[1] < 0) {
loc += "0"
} else {
loc += "1"
}
console.log(loc)
switch (loc) {
case "00":
loc += "0"
//(Bx - Ax) * (Cy - Ay) - (By - Ay) * (Cx - Ax)
cen[0]+max[0], cen[1]+max[1]
if ( max[0]*(src[1]-cen[1]) - max[1]*(src[0]-cen[0])< 0) {
console.log("exact 3")
index = 3
} else {
console.log("exact 19")
index = 19
}
break;
case "01":
loc += "1"
if ( -max[0]*(src[1]-cen[1]) - max[1]*(src[0]-cen[0])< 0) {
console.log("exact 14")
index = 14
} else {
console.log("exact 18")
index = 18
}
break;
case "10":
loc += "2"
if ( -max[0]*(src[1]-cen[1]) - max[1]*(src[0]-cen[0])< 0) {
console.log("exact 8")
index = 8
} else {
console.log("exact 4")
index = 4
}
break;
case "11":
loc += "3"
if ( max[0]*(src[1]-cen[1]) - max[1]*(src[0]-cen[0])< 0) {
console.log("exact 9")
index = 9
} else {
console.log("exact 13")
index = 13
}
break;
default:
console.log("q")
}
patharray.splice(index,0,["L",src[0],src[1]])
console.log(loc)
//console.log(patharray)
var dpath = diag.path(patharray).attr({fill: '#00f',opacity:0.1})
}
function draw_text(dialog){
//var text = svg.text(text).cx((box[2]+box[0])/2).cy((box[3]+box[1])/2)
var font = font1
var text = dialog.text
var scale = 1 / font.unitsPerEm * fontSizePx;
//console.log(1/scale)
// Layout some text - notice everything is in em units!
var result = computeLayout(font, text, {
lineHeight: 1.2 * font.unitsPerEm, // '2.5em' in font units
width: box.width / scale, // '500px' in font units
align: "center"
});
//console.log(font.getPath(text, 0, 0, fontSizePx).toPathData(1))
// Array of characters after layout
//console.log(result);
// Computed height after word-wrap
//console.log(result.height);
//console.log(result.glyphs)
var pt = []
for (ndx in result.glyphs){
if (0-result.glyphs[ndx].position[1]*scale+box.y1 > box.y2) {
fontSizePx = fontSizePx - 1
//console.log(fontSizePx)
//draw_bubble(dialog)
//draw_text(dialog)
return false
}
pt[ndx] = result.glyphs[ndx].data.getPath(result.glyphs[ndx].position[0]*scale+box.x1,0-result.glyphs[ndx].position[1]*scale+box.y1,fontSizePx).toPathData(1);
}
if (0-result.glyphs[ndx].position[1]*scale+box.y1 < box.y2 - 50) {
fontSizePx = fontSizePx + 1
//console.log(fontSizePx)
//draw_bubble(dialog)
//draw_text(dialog)
return false
}
/*
var path = result.glyphs.map((g) => {
console.log(0-g.position[1]*scale+box.y1, box.y2)
if (0-g.position[1]*scale+box.y1 > box.y2) {
fontSizePx = fontSizePx - 2
console.log(fontSizePx)
draw_bubble(dialog)
draw_text(dialog)
return
}
return g.data.getPath(g.position[0]*scale+box.x1,0-g.position[1]*scale+box.y1,fontSizePx).toPathData(1);
})
*/
//console.log(path)
//console.log(path.join(' '))
//OpenTypeToCubic(result.glyphs)
var d = pt.join(' ')
var text2 = diag.path(d)
return text2
}
//function draw_
draw_bubble(dialog)
draw_controls(dialog)
</script>
</body>
</html>