quantize-movement.js

total 0
used 0
limit 0
/* title: Quantize Highlight categories: quantize files: head point pointlist stage mouse dragging ../point_src/line.js ../point_src/random.js --- Select a target line, with quantized _snapping_ of the mouse point when near a line. */ class MainStage extends Stage { // canvas = document.getElementById('playspace'); canvas = 'playspace' mounted() { this.lines = [] this.ps = PointList.generate.random(20, [800, 10], [50, 100]) this.ps.forEach(p => { let l = new Line( [p.x, 0] , [p.x, this.dimensions.height] ) this.lines.push(l) }) } firstDraw(ctx) { // ctx.lineCap = 'round' ctx.strokeStyle = '#333' } draw(ctx){ this.clear(ctx) /* Target track the mouse, locking when near a line. */ let mp = this.mouse.point let x = ~~mp.x let snapX = mp.x // The final target line . let tLine = undefined; // A stack of lines the mouse point is near to. let near = [] // Discover near lines. this.lines.forEach(l => { // Gather the X of the line, as an int let lx = ~~l.a.x // ensure the default color is set l.color = '#444' // candidate line - if we're near the line let snap = 10 if (lx + snap >= x && lx - snap <= x) { near.push(l) } }) // Choose the closest line let _d = snapX + 10 near.forEach(l => { let _e = Math.abs(x - l.a.x) if (_e < _d) { snapX = l.a.x tLine = l; _d = Math.abs(x - l.a.x) } }) if (tLine) { tLine.color = 'pink' } // this.ps.pen.circle(ctx) this.lines.forEach(l => { l.render(ctx) }) mp.x = snapX mp.pen.fill(ctx, 'pink', 4) } } stage = MainStage.go(/*{ loop: true }*/)
Run
Meta Data
title Quantize Highlight
imports ()
files ('head', 'point', 'pointlist', 'stage', 'mouse', 'dragging', '../point_src/line.js', '../point_src/random.js')
unused_keys ()
unknown_keys ('categories',)
categories ['quantize']
filepath_exists True
path quantize-movement.js
filepath quantize-movement.js
clean_files ('../point_src/core/head.js', '../point_src/pointpen.js', '../point_src/compass.js', '../point_src/center.js', '../point_src/point-content.js', '../point_src/pointdraw.js', '../point_src/relative-xy.js', '../point_src/pointcast.js', '../point_src/point.js', '../point_src/pointlistdraw.js', '../point_src/pointlistgradient.js', '../point_src/pointlistshape.js', '../point_src/pointlistgenerator.js', '../point_src/unpack.js', '../point_src/pointlist.js', '../point_src/pointlistpen.js', '../point_src/stage-hooks.js', '../point_src/stage-resize.js', '../point_src/functions/resolve.js', '../point_src/stage.js', '../point_src/events.js', '../point_src/automouse.js', '../point_src/functions/clamp.js', '../point_src/distances.js', '../point_src/protractor.js', '../point_src/text/beta.js', '../point_src/dragging.js', '../point_src/line.js', '../point_src/random.js')
markdown {'html': '<p>Select a target line, with quantized <em>snapping</em> of the mouse point when\nnear a line.</p>', 'content': 'title: Quantize Highlight\ncategories: quantize\nfiles:\n head\n point\n pointlist\n stage\n mouse\n dragging\n ../point_src/line.js\n ../point_src/random.js\n---\n\nSelect a target line, with quantized _snapping_ of the mouse point when\nnear a line.'}