aboutsummaryrefslogtreecommitdiffstats
path: root/root/experiments
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2018-11-06 05:55:32 +0000
committers-ol <s-ol@users.noreply.github.com>2018-11-06 05:57:09 +0000
commit5be83254486bef58f85f43dba719b674f0c3bc92 (patch)
tree519608d655497a60e47202504aaed6bd98655717 /root/experiments
parentmmm.component fix (diff)
downloadmmm-5be83254486bef58f85f43dba719b674f0c3bc92.tar.gz
mmm-5be83254486bef58f85f43dba719b674f0c3bc92.zip
implement mmmfs on real fs
Diffstat (limited to 'root/experiments')
-rw-r--r--root/experiments/center_of_mass.moon179
-rw-r--r--root/experiments/center_of_mass/description: text$plain1
-rw-r--r--root/experiments/center_of_mass/text$moonscript -> mmm$dom.moon171
-rw-r--r--root/experiments/init.moon27
-rw-r--r--root/experiments/tags/description: text$plain1
-rw-r--r--root/experiments/tags/init.moon99
-rw-r--r--root/experiments/tags/tags: text$moonscript -> table.moon (renamed from root/experiments/tags/tags.moon)0
-rw-r--r--root/experiments/tags/text$moonscript -> fn -> mmm$component.moon92
-rw-r--r--root/experiments/text$moonscript -> fn -> mmm$dom.moon18
-rw-r--r--root/experiments/title: text$plain1
10 files changed, 284 insertions, 305 deletions
diff --git a/root/experiments/center_of_mass.moon b/root/experiments/center_of_mass.moon
deleted file mode 100644
index 41cf8a4..0000000
--- a/root/experiments/center_of_mass.moon
+++ /dev/null
@@ -1,179 +0,0 @@
-import define_fileders from require 'mmm.mmmfs'
-Fileder = define_fileders ...
-
-with Fileder {
- 'name: alpha': 'center_of_mass',
- 'description: text/plain': "Fonts aligned by Center-of-Mass",
-}
-
- if MODE == 'CLIENT'
- import CanvasApp from require 'mmm.canvasapp'
- import rgb from require 'mmm.color'
- import article, h1, p, div, span, input, button from require 'mmm.dom'
-
- fast = true
- center = true
- center_char = do
- canvas = document\createElement 'canvas'
- ctx = canvas\getContext '2d'
-
- cache = {}
-
- (char, font, height) ->
- name = "#{char} #{height} #{font}"
- return table.unpack cache[name] if cache[name]
-
- ctx\resetTransform!
- ctx.font = "#{height}px #{font}"
- width = (ctx\measureText char).width
- canvas.width, canvas.height = width, height * 1.2
-
- ctx.font = "#{height}px #{font}"
- ctx.textBaseline = 'top'
- ctx.fillStyle = rgb 0, 0, 0
- ctx\fillText char, 0, 0
-
- data = ctx\getImageData 0, 0, width, height * 1.2
-
- local xx, yy
- if fast
- loop = window\eval '(function(data) {
- var xx = 0, yy = 0, n = 0;
- for (var x = 0; x < data.width - 1; x++) {
- for (var y = 0; y < data.height - 1; y++) {
- var i = y * (data.width * 4) + x * 4;
- var alpha = data.data[i + 3] / 255;
- xx += x * alpha;
- yy += y * alpha;
- n += alpha;
- }
- }
-
- xx /= n;
- yy /= n;
- return [xx, yy];
- })'
- res = loop nil, data
- xx, yy = res[0], res[1]
- else
- xx, yy, n = 0, 0, 0
- for x = 0, data.width - 1
- for y = 0, data.height - 1
- i = y * (data.width * 4) + x * 4
- alpha = data.data[i + 3] / 255
- xx += x * alpha
- yy += y * alpha
- n += alpha
-
- xx /= n
- yy /= n
- cache[name] = { xx, yy, width }
- xx, yy, width
-
- class CenterOfMass extends CanvasApp
- width: window.innerWidth - 20
- height: 300
- new: (text, @font, @size) =>
- super!
- @text = {}
- for i = 1,#text
- @add text\sub i, i
-
- add: (char) =>
- rcx, rcy, w = center_char char, @font, @size
- cx, cy = w/2, @size/2
- vx, vy = 0, 0
- table.insert @text, {
- :char, :rcx, :rcy, :w
- :cx, :cy, :vx, :vy
- }
-
- refresh: =>
- for char in *@text
- char.rcx, char.rcy, char.w = center_char char.char, @font, @size
-
- keydown: (key) =>
- if key == "Backspace" or key == "Delete"
- table.remove @text
- elseif string.len(key) == 1
- @add key
-
- update: (dt) =>
- super dt
-
- ACCEL = 4 * dt
- DAMPING = 8 * dt
-
- for char in *@text
- { :rcx, :rcy, :cx, :cy, :w } = char
- if not center
- rcx, rcy = w/2, @size/2
- dx, dy = rcx - cx, rcy - cy
- char.vx += dx * ACCEL
- char.vy += dy * ACCEL
- char.cx += char.vx
- char.cy += char.vy
- char.vx *= DAMPING
- char.vy *= DAMPING
-
- draw: =>
- @ctx\clearRect 0, 0, @width, @height
-
- @ctx.font = "#{@size}px #{@font}"
- @ctx.textBaseline = 'top'
-
- x, y = @size * .1, @size
- for { :char, :cx, :cy, :w } in *@text
- if x + w > @width
- x = 0
- y += @size * 1.2
-
- @ctx\fillText char, x + w/2 - cx, y - cy
- x += w
-
- .props['fn -> mmm/dom'] = ->
- _content = {}
- append = (x) -> table.insert _content, x
-
- append h1 'Fonts aligned by Center-of-Mass'
- app = CenterOfMass "Click here and type Away!", "Times New Roman", 40
- append app.canvas
- app.canvas.style.backgroundColor = '#eee'
-
- add = =>
- append div {
- span 'font: ',
- with @font_input = input!
- .type = 'text'
- .value = 'Times New Roman'
- with button 'set'
- .onclick = (_, e) ->
- app.font = @font_input.value
- app\refresh!
- }
-
- append div {
- span 'size: ',
- input type: 'range', min: 2, max: 120, value: 40, onchange: (_, e) ->
- size = e.target.value
- @size_label.innerText = size
- app.size = size
- app\refresh!
- with @size_label = span '40'
- ''
- }
-
- append div {
- span 'center characters by weight: ',
- input type: 'checkbox', checked: center, onchange: (_, e) ->
- center = e.target.checked
- }
-
- append div {
- span 'optimize inner loop: ',
- input type: 'checkbox', checked: fast, onchange: (_, e) ->
- fast = e.target.checked
- }
- add {}
-
- article _content
diff --git a/root/experiments/center_of_mass/description: text$plain b/root/experiments/center_of_mass/description: text$plain
new file mode 100644
index 0000000..256df66
--- /dev/null
+++ b/root/experiments/center_of_mass/description: text$plain
@@ -0,0 +1 @@
+Fonts aligned by Center-of-Mass
diff --git a/root/experiments/center_of_mass/text$moonscript -> mmm$dom.moon b/root/experiments/center_of_mass/text$moonscript -> mmm$dom.moon
new file mode 100644
index 0000000..a43ea69
--- /dev/null
+++ b/root/experiments/center_of_mass/text$moonscript -> mmm$dom.moon
@@ -0,0 +1,171 @@
+assert MODE == 'CLIENT', '[nossr]'
+
+import CanvasApp from require 'mmm.canvasapp'
+import rgb from require 'mmm.color'
+import article, h1, p, div, span, input, button from require 'mmm.dom'
+
+fast = true
+center = true
+center_char = do
+ canvas = document\createElement 'canvas'
+ ctx = canvas\getContext '2d'
+
+ cache = {}
+
+ (char, font, height) ->
+ name = "#{char} #{height} #{font}"
+ return table.unpack cache[name] if cache[name]
+
+ ctx\resetTransform!
+ ctx.font = "#{height}px #{font}"
+ width = (ctx\measureText char).width
+ canvas.width, canvas.height = width, height * 1.2
+
+ ctx.font = "#{height}px #{font}"
+ ctx.textBaseline = 'top'
+ ctx.fillStyle = rgb 0, 0, 0
+ ctx\fillText char, 0, 0
+
+ data = ctx\getImageData 0, 0, width, height * 1.2
+
+ local xx, yy
+ if fast
+ loop = window\eval '(function(data) {
+ var xx = 0, yy = 0, n = 0;
+ for (var x = 0; x < data.width - 1; x++) {
+ for (var y = 0; y < data.height - 1; y++) {
+ var i = y * (data.width * 4) + x * 4;
+ var alpha = data.data[i + 3] / 255;
+ xx += x * alpha;
+ yy += y * alpha;
+ n += alpha;
+ }
+ }
+
+ xx /= n;
+ yy /= n;
+ return [xx, yy];
+ })'
+ res = loop nil, data
+ xx, yy = res[0], res[1]
+ else
+ xx, yy, n = 0, 0, 0
+ for x = 0, data.width - 1
+ for y = 0, data.height - 1
+ i = y * (data.width * 4) + x * 4
+ alpha = data.data[i + 3] / 255
+ xx += x * alpha
+ yy += y * alpha
+ n += alpha
+
+ xx /= n
+ yy /= n
+ cache[name] = { xx, yy, width }
+ xx, yy, width
+
+class CenterOfMass extends CanvasApp
+ width: window.innerWidth - 20
+ height: 300
+ new: (text, @font, @size) =>
+ super!
+ @text = {}
+ for i = 1,#text
+ @add text\sub i, i
+
+ add: (char) =>
+ rcx, rcy, w = center_char char, @font, @size
+ cx, cy = w/2, @size/2
+ vx, vy = 0, 0
+ table.insert @text, {
+ :char, :rcx, :rcy, :w
+ :cx, :cy, :vx, :vy
+ }
+
+ refresh: =>
+ for char in *@text
+ char.rcx, char.rcy, char.w = center_char char.char, @font, @size
+
+ keydown: (key) =>
+ if key == "Backspace" or key == "Delete"
+ table.remove @text
+ elseif string.len(key) == 1
+ @add key
+
+ update: (dt) =>
+ super dt
+
+ ACCEL = 4 * dt
+ DAMPING = 8 * dt
+
+ for char in *@text
+ { :rcx, :rcy, :cx, :cy, :w } = char
+ if not center
+ rcx, rcy = w/2, @size/2
+ dx, dy = rcx - cx, rcy - cy
+ char.vx += dx * ACCEL
+ char.vy += dy * ACCEL
+ char.cx += char.vx
+ char.cy += char.vy
+ char.vx *= DAMPING
+ char.vy *= DAMPING
+
+ draw: =>
+ @ctx\clearRect 0, 0, @width, @height
+
+ @ctx.font = "#{@size}px #{@font}"
+ @ctx.textBaseline = 'top'
+
+ x, y = @size * .1, @size
+ for { :char, :cx, :cy, :w } in *@text
+ if x + w > @width
+ x = 0
+ y += @size * 1.2
+
+ @ctx\fillText char, x + w/2 - cx, y - cy
+ x += w
+
+_content = {}
+append = (x) -> table.insert _content, x
+
+append h1 'Fonts aligned by Center-of-Mass'
+app = CenterOfMass "Click here and type Away!", "Times New Roman", 40
+append app.canvas
+app.canvas.style.backgroundColor = '#eee'
+
+add = =>
+ append div {
+ span 'font: ',
+ with @font_input = input!
+ .type = 'text'
+ .value = 'Times New Roman'
+ with button 'set'
+ .onclick = (_, e) ->
+ app.font = @font_input.value
+ app\refresh!
+ }
+
+ append div {
+ span 'size: ',
+ input type: 'range', min: 2, max: 120, value: 40, onchange: (_, e) ->
+ size = e.target.value
+ @size_label.innerText = size
+ app.size = size
+ app\refresh!
+ with @size_label = span '40'
+ ''
+ }
+
+ append div {
+ span 'center characters by weight: ',
+ input type: 'checkbox', checked: center, onchange: (_, e) ->
+ center = e.target.checked
+ }
+
+ append div {
+ span 'optimize inner loop: ',
+ input type: 'checkbox', checked: fast, onchange: (_, e) ->
+ fast = e.target.checked
+ }
+add {}
+
+article _content
diff --git a/root/experiments/init.moon b/root/experiments/init.moon
deleted file mode 100644
index ad3e06a..0000000
--- a/root/experiments/init.moon
+++ /dev/null
@@ -1,27 +0,0 @@
-import div, h3, ul, li, a from require 'mmm.dom'
-import define_fileders from require 'mmm.mmmfs'
-Fileder = define_fileders ...
-require = relative ...
-
-Fileder {
- 'name: alpha': 'experiments',
- 'title: text/plain': 'various experiments',
- 'fn -> mmm/dom': (path) => div {
- h3 @gett 'title: text/plain', style: { 'margin-bottom': '-.5em' },
- ul for child in *@children
- name = child\gett 'name: alpha'
- desc = child\gett 'description: text/plain'
- li {
- a name, {
- href: child.path,
- onclick: (e) =>
- e\preventDefault!
- BROWSER\navigate child.path
- },
- ': ', desc
- }
- }
-
- require '.center_of_mass'
- require '.tags'
-}
diff --git a/root/experiments/tags/description: text$plain b/root/experiments/tags/description: text$plain
new file mode 100644
index 0000000..e585bb8
--- /dev/null
+++ b/root/experiments/tags/description: text$plain
@@ -0,0 +1 @@
+defining toggles, categories etc. with tags and functional hooks
diff --git a/root/experiments/tags/init.moon b/root/experiments/tags/init.moon
deleted file mode 100644
index 6b7945b..0000000
--- a/root/experiments/tags/init.moon
+++ /dev/null
@@ -1,99 +0,0 @@
-import div, h3, ul, li, a from require 'mmm.dom'
-import define_fileders from require 'mmm.mmmfs'
-Fileder = define_fileders ...
-require = relative ...
-
-Fileder {
- 'name: alpha': 'tags',
- 'description: text/plain': 'defining toggles, categories etc. with only tags and functional hooks',
- 'fn -> mmm/component': if MODE == 'CLIENT' then =>
- import add_tag, rmv_tag, Node, Hierarchy, Toggle, NamespacedToggle from require '.tags'
- import ReactiveVar, tohtml, text, elements from require 'mmm.component'
- import article, div, form, span, h3, a, input, textarea, button from elements
-
- clone = (set) ->
- assert set and 'table' == (type set), 'not a set'
- { k,v for k,v in pairs set }
-
- set_append = (val) -> (set) ->
- with copy = clone set
- copy[val] = val
-
- set_remove = (val) -> (set) ->
- with copy = clone set
- copy[val] = nil
-
- set_join = (tbl, sep=' ') ->
- ret = ''
- for tag in pairs tbl
- ret ..= (tostring tag) .. sep
- ret
-
- entries = div!
-
- class ReactiveNode extends Node
- new: (...) =>
- super ...
- @tags = ReactiveVar @tags
- @_node = div {
- span @name, style: { 'font-weight': 'bold' },
- @tags\map (tags) -> with div!
- for tag,_ in pairs tags
- \append a (text tag), href: '#', style: {
- display: 'inline-block',
- margin: '0 5px',
- }
- }
-
- @node = tohtml @_node
-
- has: (tag) => @tags\get![tag]
- add: (tag) => @tags\transform set_append tag
- rmv: (tag) => @tags\transform set_remove tag
-
- rules = {
- Hierarchy 'home', 'sol'
- Hierarchy 'sol', 'desktop'
- Hierarchy 'desktop', 'vacation'
- Hierarchy 'desktop', 'documents'
- NamespacedToggle 'documents', 'work', 'personal'
- -- Toggle 'work', 'personal'
- -- Hierarchy 'documents', 'work'
- -- Hierarchy 'documents', 'personal'
- }
-
- pictures = for i=1,10
- with node = ReactiveNode "picture#{i}.jpg"
- entries\append node
- add_tag node, 'vacation'
-
- pers = ReactiveNode 'mypersonalfile.doc'
- entries\append pers
-
- article entries, div do
- yield = coroutine.yield
- step = coroutine.wrap ->
- yield "mark document"
- add_tag pers, 'documents'
-
- yield "mark personal"
- add_tag pers, 'personal'
-
- yield "mark work"
- add_tag pers, 'work'
-
- yield "unmark work"
- rmv_tag pers, 'work'
-
- yield "remove from documents"
- rmv_tag pers, 'documents'
-
- yield false
-
- next_step = ReactiveVar step!
- next_step\map (desc) ->
- if desc
- button (text desc), onclick: (e) => next_step\set step!
- else
- text ''
-}
diff --git a/root/experiments/tags/tags.moon b/root/experiments/tags/tags: text$moonscript -> table.moon
index 198392e..198392e 100644
--- a/root/experiments/tags/tags.moon
+++ b/root/experiments/tags/tags: text$moonscript -> table.moon
diff --git a/root/experiments/tags/text$moonscript -> fn -> mmm$component.moon b/root/experiments/tags/text$moonscript -> fn -> mmm$component.moon
new file mode 100644
index 0000000..24972ac
--- /dev/null
+++ b/root/experiments/tags/text$moonscript -> fn -> mmm$component.moon
@@ -0,0 +1,92 @@
+=>
+ assert MODE == 'CLIENT', '[nossr]'
+
+ import add_tag, rmv_tag, Node, Hierarchy, Toggle, NamespacedToggle from @get 'tags: table'
+ import ReactiveVar, tohtml, text, elements from require 'mmm.component'
+ import article, div, form, span, h3, a, input, textarea, button from elements
+
+ clone = (set) ->
+ assert set and 'table' == (type set), 'not a set'
+ { k,v for k,v in pairs set }
+
+ set_append = (val) -> (set) ->
+ with copy = clone set
+ copy[val] = val
+
+ set_remove = (val) -> (set) ->
+ with copy = clone set
+ copy[val] = nil
+
+ set_join = (tbl, sep=' ') ->
+ ret = ''
+ for tag in pairs tbl
+ ret ..= (tostring tag) .. sep
+ ret
+
+ entries = div!
+
+ class ReactiveNode extends Node
+ new: (...) =>
+ super ...
+ @tags = ReactiveVar @tags
+ @_node = div {
+ span @name, style: { 'font-weight': 'bold' },
+ @tags\map (tags) -> with div!
+ for tag,_ in pairs tags
+ \append a (text tag), href: '#', style: {
+ display: 'inline-block',
+ margin: '0 5px',
+ }
+ }
+
+ @node = tohtml @_node
+
+ has: (tag) => @tags\get![tag]
+ add: (tag) => @tags\transform set_append tag
+ rmv: (tag) => @tags\transform set_remove tag
+
+ rules = {
+ Hierarchy 'home', 'sol'
+ Hierarchy 'sol', 'desktop'
+ Hierarchy 'desktop', 'vacation'
+ Hierarchy 'desktop', 'documents'
+ NamespacedToggle 'documents', 'work', 'personal'
+ -- Toggle 'work', 'personal'
+ -- Hierarchy 'documents', 'work'
+ -- Hierarchy 'documents', 'personal'
+ }
+
+ pictures = for i=1,10
+ with node = ReactiveNode "picture#{i}.jpg"
+ entries\append node
+ add_tag node, 'vacation'
+
+ pers = ReactiveNode 'mypersonalfile.doc'
+ entries\append pers
+
+ article entries, div do
+ yield = coroutine.yield
+ step = coroutine.wrap ->
+ yield "mark document"
+ add_tag pers, 'documents'
+
+ yield "mark personal"
+ add_tag pers, 'personal'
+
+ yield "mark work"
+ add_tag pers, 'work'
+
+ yield "unmark work"
+ rmv_tag pers, 'work'
+
+ yield "remove from documents"
+ rmv_tag pers, 'documents'
+
+ yield false
+
+ next_step = ReactiveVar step!
+ next_step\map (desc) ->
+ if desc
+ button (text desc), onclick: (e) => next_step\set step!
+ else
+ text ''
diff --git a/root/experiments/text$moonscript -> fn -> mmm$dom.moon b/root/experiments/text$moonscript -> fn -> mmm$dom.moon
new file mode 100644
index 0000000..1c04309
--- /dev/null
+++ b/root/experiments/text$moonscript -> fn -> mmm$dom.moon
@@ -0,0 +1,18 @@
+import div, h3, ul, li, a from require 'mmm.dom'
+
+=>
+ div {
+ h3 @gett 'title: text/plain', style: { 'margin-bottom': '-.5em' },
+ ul for child in *@children
+ name = child\gett 'name: alpha'
+ desc = child\gett 'description: text/plain'
+ li {
+ a name, {
+ href: child.path,
+ onclick: (e) =>
+ e\preventDefault!
+ BROWSER\navigate child.path
+ },
+ ': ', desc
+ }
+ }
diff --git a/root/experiments/title: text$plain b/root/experiments/title: text$plain
new file mode 100644
index 0000000..0487531
--- /dev/null
+++ b/root/experiments/title: text$plain
@@ -0,0 +1 @@
+various experiments