Yes, it works. Would setting innerHTML be cleaner, though?
Chris Pressey
10 years ago
0 | 0 | /* |
1 | 1 | <br>% to hold fixed: |
2 | <input id="hold_fixed" type="range" min="0" max="100" value="5"/> | |
3 | 2 | */ |
4 | ||
5 | function makeCanvas(container, id, width, height) { | |
6 | var canvas = document.createElement('canvas'); | |
7 | canvas.id = id; | |
8 | canvas.width = width; | |
9 | canvas.height = height; | |
10 | container.appendChild(canvas); | |
11 | return canvas; | |
12 | } | |
13 | ||
14 | function makeButton(container, name, label) { | |
15 | var button = document.createElement('button'); | |
16 | button.id = 'btn_' + name; | |
17 | button.innerHTML = label; | |
18 | container.appendChild(button); | |
19 | return button; | |
20 | } | |
21 | 3 | |
22 | 4 | function launch(container) { |
23 | 5 | var deps = [ |
6 | "../common-yoob.js-0.6/element-factory.js", | |
24 | 7 | "../common-yoob.js-0.6/animation.js" |
25 | 8 | ]; |
26 | 9 | var loaded = 0; |
30 | 13 | elem.onload = function() { |
31 | 14 | if (++loaded == deps.length) { |
32 | 15 | container = document.getElementById('container'); |
33 | var canvas = makeCanvas(container, 'canvas', 500, 500); | |
34 | var button = makeButton(container, 'restart', 'Restart'); | |
16 | var canvas = yoob.makeCanvas(container, 500, 500); | |
17 | canvas.style.display = "inline-block"; | |
18 | container.appendChild(document.createElement('br')); | |
19 | var button = yoob.makeButton(container, 'Restart'); | |
20 | container.appendChild(document.createTextNode("Percent to hold fixed:")); | |
21 | var slider = yoob.makeSlider(container, 0, 100, 5); | |
35 | 22 | var t = new Chzrxl(); |
36 | 23 | t.init({ |
37 | 24 | 'canvas': canvas, |
38 | 'button': button | |
25 | 'restartButton': button, | |
26 | 'pctFixedSlider': slider | |
39 | 27 | }); |
40 | 28 | } |
41 | 29 | }; |
96 | 84 | this.init = function(opts) { |
97 | 85 | options = opts || {}; |
98 | 86 | |
99 | canvas = opts.canvas; | |
87 | canvas = options.canvas; | |
100 | 88 | ctx = canvas.getContext("2d"); |
101 | 89 | |
102 | button = opts.button; | |
90 | button = options.restartButton; | |
103 | 91 | var $this = this; |
104 | 92 | button.onclick = function() { |
105 | 93 | $this.restart(); |
106 | 94 | }; |
107 | 95 | |
108 | 96 | percentToHoldFixed = options.percentToHoldFixed || 10; |
109 | pctToHoldFixedCtrl = document.getElementById('hold_fixed'); | |
97 | pctToHoldFixedCtrl = options.pctFixedSlider; | |
98 | pctToHoldFixedCtrl.value = percentToHoldFixed; | |
99 | ||
110 | 100 | this.restart(); |
111 | 101 | this.animation = (new yoob.Animation()).init({ |
112 | 102 | object: this |
2 | 2 | <meta charset="utf-8"> |
3 | 3 | <title>Chzrxl</title> |
4 | 4 | <style> |
5 | #canvas { border: 1px solid blue; } | |
5 | canvas { border: 1px solid blue; } | |
6 | #container { | |
7 | width: 100%; | |
8 | text-align: center; | |
9 | } | |
6 | 10 | </style> |
7 | 11 | </head> |
8 | 12 | <body> |
9 | 13 | |
10 | 14 | <h1>Chzrxl</h1> |
11 | 15 | |
12 | <!-- | |
13 | <br>% to hold fixed: | |
14 | <input id="hold_fixed" type="range" min="0" max="100" value="5"/> | |
15 | <button id="restart" onclick='t.restart()'>Restart</button> | |
16 | <div id="info"></div> | |
17 | --> | |
18 | ||
19 | <div id='container'></div> | |
16 | <div id="container"></div> | |
20 | 17 | |
21 | 18 | </body> |
22 | 19 | <script src="chzrxl.js"></script> |
0 | /* | |
1 | * This file is part of yoob.js version 0.7-PRE | |
2 | * Available from https://github.com/catseye/yoob.js/ | |
3 | * This file is in the public domain. See http://unlicense.org/ for details. | |
4 | */ | |
5 | if (window.yoob === undefined) yoob = {}; | |
6 | ||
7 | /* | |
8 | * Functions for creating elements. | |
9 | * | |
10 | * I dunno -- maybe just setting innerHTML would be better. | |
11 | */ | |
12 | ||
13 | yoob.makeCanvas = function(container, width, height) { | |
14 | var canvas = document.createElement('canvas'); | |
15 | canvas.width = width; | |
16 | canvas.height = height; | |
17 | container.appendChild(canvas); | |
18 | return canvas; | |
19 | }; | |
20 | ||
21 | yoob.makeButton = function(container, label) { | |
22 | var button = document.createElement('button'); | |
23 | button.innerHTML = label; | |
24 | container.appendChild(button); | |
25 | return button; | |
26 | }; | |
27 | ||
28 | yoob.makeCheckbox = function(container, name, labelText) { | |
29 | var checkbox = document.createElement('input'); | |
30 | checkbox.type = "checkbox"; | |
31 | checkbox.id = 'cb_' + name; | |
32 | checkbox.checked = false; | |
33 | var label = document.createElement('label'); | |
34 | label.htmlFor = 'cb_' + name; | |
35 | label.appendChild(document.createTextNode(labelText)); | |
36 | ||
37 | container.appendChild(checkbox); | |
38 | container.appendChild(label); | |
39 | ||
40 | return checkbox; | |
41 | }; | |
42 | ||
43 | yoob.makeSlider = function(container, min, max, value) { | |
44 | var slider = document.createElement('input'); | |
45 | slider.type = "range"; | |
46 | slider.min = min; | |
47 | slider.max = max; | |
48 | slider.value = value; | |
49 | container.appendChild(slider); | |
50 | return slider; | |
51 | }; |