aboutsummaryrefslogtreecommitdiffstats
path: root/root/blog/aspect_ratios/interactive/_base: text$moonscript -> table.moon
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2019-07-12 15:19:55 +0000
committers-ol <s-ol@users.noreply.github.com>2019-07-12 15:19:55 +0000
commitf0b8e51fb9cac674d6f892db116e9d37524f6b16 (patch)
tree589a0be3c16e452d12fe167a181f58c96f9571c0 /root/blog/aspect_ratios/interactive/_base: text$moonscript -> table.moon
parentfix sassc mismatch (diff)
downloadmmm-f0b8e51fb9cac674d6f892db116e9d37524f6b16.tar.gz
mmm-f0b8e51fb9cac674d6f892db116e9d37524f6b16.zip
add aspect ratios
Diffstat (limited to 'root/blog/aspect_ratios/interactive/_base: text$moonscript -> table.moon')
-rw-r--r--root/blog/aspect_ratios/interactive/_base: text$moonscript -> table.moon92
1 files changed, 92 insertions, 0 deletions
diff --git a/root/blog/aspect_ratios/interactive/_base: text$moonscript -> table.moon b/root/blog/aspect_ratios/interactive/_base: text$moonscript -> table.moon
new file mode 100644
index 0000000..d348f4b
--- /dev/null
+++ b/root/blog/aspect_ratios/interactive/_base: text$moonscript -> table.moon
@@ -0,0 +1,92 @@
+assert MODE == 'CLIENT', '[nossr]'
+
+import CanvasApp from require 'mmm.canvasapp'
+import div from require 'mmm.dom'
+
+class UIDemo extends CanvasApp
+ width: nil
+ height: nil
+ new: () =>
+ super!
+
+ @canvas.width = nil
+ @canvas.height = nil
+ @canvas.style.width = '100%'
+ @canvas.style.height = '100%'
+ @canvas.style.border = '2px solid var(--gray-dark)'
+
+ @node = div @canvas, style: {
+ position: 'relative'
+ resize: 'horizontal'
+ overflow: 'hidden'
+
+ width: '576px'
+ height: '324px'
+ minWidth: '324px'
+ maxWidth: '742px'
+
+ margin: 'auto'
+ padding: '10px'
+ boxSizing: 'border-box'
+ }
+
+ -- match size of current parent element and return it (for interactive resizing in the demo)
+ updateSize: =>
+ { :clientWidth, :clientHeight } = @canvas.parentElement
+ @canvas.width, @canvas.height = clientWidth, clientHeight
+ @canvas.width, @canvas.height
+
+ -- set up a coordinate system such that the virtual viewport
+ -- of size (w, h) is centered on (0,0) and fills the canvas
+ -- returns remaining margins on the two axes
+ fit: (w, h) =>
+ width, height = @updateSize!
+ @ctx\translate width/2, height/2
+
+ -- maximum scale without cropping either axis
+ scale = math.min (width/w), (height/h)
+ @ctx\scale scale, scale
+
+ -- calculate remaining space on x/y axis
+ rx = (width/scale) - w
+ ry = (height/scale) - h
+
+ -- margins are half of the remaining space
+ rx / 2, ry / 2
+
+ strokeRect: (cx, cy, w, h) =>
+ lw = @ctx.lineWidth / 2
+ @ctx\strokeRect cx - w/2 + lw, cy - h/2 + lw,
+ w - 2*lw, h - 2*lw,
+
+class Box
+ new: (@cx, @cy, @w, @h) =>
+
+ rect: =>
+ @cx, @cy, @w, @h
+
+class Example extends UIDemo
+ click: =>
+ if @naive
+ @naive = false
+ else
+ if @show_boxes
+ @naive = true
+ @show_boxes = not @show_boxes
+
+ text: (box, text, align='center', my=.5) =>
+ mx = .1
+ @ctx.textAlign = align
+
+ if align == 'left'
+ @ctx\fillText text, box.cx + mx - box.w/2, box.cy + my
+ else if align == 'center'
+ @ctx\fillText text, box.cx, box.cy + my
+ if align == 'right'
+ @ctx\fillText text, box.cx - mx + box.w/2, box.cy + my
+
+{
+ :UIDemo
+ :Example
+ :Box
+}