vector-field.js

total 0
used 0
limit 0
/* title: Example categories: velocity files: ../point_src/core/head.js ../point_src/pointpen.js ../point_src/pointdraw.js ../point_src/math.js ../point_src/extras.js ../point_src/point-content.js ../point_src/pointlist.js ../point_src/pointlistpen.js ../point_src/point.js ../point_src/stage.js mouse dragging stroke ../point_src/velocity.js --- */ class MainStage extends Stage { canvas = 'playspace' mounted(){ let rowCount = 20 this.ps = PointList.generate.grid(200, rowCount, 30) this.gt = this.ps.generate.getGridTool(rowCount) this.ps.each.radius = 10 this.dragging.add(...this.ps) this.ps.forEach(p=> p.velocity.set(1,1)) console.log('mounted') this.actives = [] this.subVal = .99 } onMousedown(ev) { let p = this.dragging.getPoint() p && this.pushVelocities(p) } pushVelocities(origin) { let power = 20 let ps = this.ps; let i = ps.indexOf(origin) pushVelocities(ps, this.gt, origin) } step() { let gt = this.gt let ps = this.ps let actives = this.actives // let newActives = [] ps.forEach(function(p, i){ let siblings = gt.getSiblings(i) // p.velocity.mutableMul({x:.99, y:.99}) // let p = ps[i] let v = p.velocity /* Add all the velocities together. */ let c = 1 siblings.forEach(function(j){ let ot = ps[j].velocity if(ot.x + ot.y < 0.3) { return /* skip blanks */ } c += 1 v = v.add(ot) }) /* Then divide to add to _self_. */ v = v.div({x:c, y:c}) v.x = clamp(v.x, 1, 10) v.y = clamp(v.y, 1, 10) p.velocity.copy(v) // newActives = newActives.concat(siblings) }) } draw(ctx){ this.clear(ctx) // this.step() this.ps.pen.circle(ctx, undefined, 'purple') let subVal = this.subVal this.ps.forEach(function(p){ p.pen.line(ctx, p.add(p.velocity), 'red') // p.velocity.mutableSub({x:subVal, y:subVal}) p.velocity.mutableMul({x:subVal, y:subVal}) }) } } const pushVelocities = function(ps, gt, origin) { let power = 20; // initial velocity magnitude let damping = 0.8; // how much velocity is reduced each neighbor let minThreshold = 0.1; // cutoff to stop pushing once the velocity is tiny // let ps = this.ps;d let originIndex = ps.indexOf(origin); // Keep track of points we’ve processed let visited = new Set(); // Our BFS queue: each entry has { idx, strength } let queue = [{ idx: originIndex, strength: power }]; // Process until we exhaust the queue while (queue.length > 0) { let { idx, strength } = queue.shift(); // Skip if we've seen this point or if strength is too small if (visited.has(idx) || strength < minThreshold) continue; visited.add(idx); let current = ps[idx]; // For each neighbor, add velocity let neighbors = gt.getSiblings(idx); neighbors.forEach(neighIdx => { if (!visited.has(neighIdx)) { let neighbor = ps[neighIdx]; if(!neighbor) { return } // Direction from current point to neighbor let angleRads = current.directionTo(neighbor); // Add velocity in that direction let add = { x: Math.cos(angleRads) * strength, y: Math.sin(angleRads) * strength }; neighbor.velocity.mutableAdd(add); // Enqueue neighbor to continue the“wave queue.push({ idx: neighIdx, strength: strength * damping }); } }); } } stage = MainStage.go(/*{ loop: true }*/)
Run
Meta Data
title Example
imports ()
files ('../point_src/core/head.js', '../point_src/pointpen.js', '../point_src/pointdraw.js', '../point_src/math.js', '../point_src/extras.js', '../point_src/point-content.js', '../point_src/pointlist.js', '../point_src/pointlistpen.js', '../point_src/point.js', '../point_src/stage.js', 'mouse', 'dragging', 'stroke', '../point_src/velocity.js')
unused_keys ()
unknown_keys ('categories',)
categories ['velocity']
filepath_exists True
path vector-field.js
filepath vector-field.js
clean_files ('../point_src/core/head.js', '../point_src/pointpen.js', '../point_src/pointdraw.js', '../point_src/math.js', '../point_src/extras.js', '../point_src/compass.js', '../point_src/center.js', '../point_src/point-content.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/relative-xy.js', '../point_src/pointcast.js', '../point_src/point.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/setunset.js', '../point_src/stroke.js', '../point_src/velocity.js')
markdown {'html': '', 'content': 'title: Example\ncategories: velocity\nfiles:\n ../point_src/core/head.js\n ../point_src/pointpen.js\n ../point_src/pointdraw.js\n ../point_src/math.js\n ../point_src/extras.js\n ../point_src/point-content.js\n ../point_src/pointlist.js\n ../point_src/pointlistpen.js\n ../point_src/point.js\n ../point_src/stage.js\n mouse\n dragging\n stroke\n ../point_src/velocity.js\n---'}