A modular visualization system for JavaScript.
gl-vis / regl-line2d Goto Github PK
View Code? Open in Web Editor NEW:part_alternation_mark: Draw 2d polyline with regl
Home Page: https://gl-vis.github.io/regl-line2d
License: MIT License
:part_alternation_mark: Draw 2d polyline with regl
Home Page: https://gl-vis.github.io/regl-line2d
License: MIT License
For now reverse direction is detected well on full screen, but not when viewport is set.
Something special with adjacent miters happen.
We probably want to disable miters for obtuse angles.
Related to #12
Distance-based dash is more svg-ish but prone to errors on big data, position-based dash is round error proof, but bad on scale.
We can switch that mode by the calculated distance.
Although it seems that we cannot track scale-independent distance.
Is it planned to allow drawing multiple poly-lines with a single draw call? i.e. GL_LINES
Should not cover miter limit
For implementing smooth spline segments it is possible to use https://github.com/stbaer/smooth-path and just map input vertices, as a simple workaround
plotly/plotly.js#3229
... or make sure it is transpiled.
See:
https://github.com/dfcreative/regl-line2d/blob/master/index.js#L563
and possibly elsewhere.
Non-ES5 statements like this one makes our (very) old image test server crash.
When drawing lines with simple shader by using join type rect
it's rendering with single color:
{
color:['red', 'yellow'],
join:'rect',
positions: [-0,0,20,10],
thickness: 10,
dash: [15, 5]
}
Adding aColor, bColor to the rect-vert.glsl
will produce correct shading
attribute vec4 aColor, bColor;
...
fragColor = (lineStart * aColor + lineEnd * bColor) / 255.;
also changing regl attributes to
aColor: {
buffer: regl.prop('colorBuffer'),
stride: 4,
offset: 0,
divisor: 1
},
bColor: {
buffer: regl.prop('colorBuffer'),
stride: 4,
offset: 4,
divisor: 1
}
Thank you.
Now sharp-angle connections render color transition with noticable shift. We have to correct that by miter limit or so.
I am a user of plotly.js
(version 1.37.1
) which depends on regl-line2d
(version ^3.0.9
). The destructuring assignment used in file index.js
in regl-line2d
is not compatibly with any version of Internet Explorer (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment ) and therefore breaks my app. I get an error script 1010 expected identifier
.
{
type: 'round',
positions: [1, -2, 2, -2, 3, NaN, 4, 2, 5, 2],
width: 20,
color: 'red'
}
Does not render stuff, unlike rect
Modified frag shader presumably tests if fragment is within miter radius, if it exceeds 0 bevel miter limit.
let regl = require('regl')({extensions: 'angle_instanced_arrays'})
let line2d = require('./')(regl)
line2d.update([
{ thickness: 4, points: [0,0, 1,1, 1,0], close: true, color: 'red' },
undefined,
{ thickness: 4, points: [0,1, 1,1, 0,0], close: true, color: 'blue' }
])
line2d.draw(0)
line2d.draw(2)
only draws the red triangle.
Example in plotly.js https://codepen.io/etpinard/pen/bKQjMe
Now cutoffs use fragment shader strategy, which overcalculates them, esp. for sharp angles or small scales. Engaging stencil buffer would allow to avoid conditions in fragment shader, in theory.
Would be more useful to have miterLimit
as a ratio to thickness according to the formula from https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-miterlimit
Get a better test for depth/stencil buffer
See https://codepen.io/anon/pen/yZpXZE?editors=1010 which results in something like
(noticed that weird final red line segment) on some (macOS?) hardware.
See plotly/plotly.js#3522 for more details.
precision
propertyJust as classical gl-line2d - it is faster than miter line
To adjust join widths by user
Probably with the API
color: colorscale
, where colorscale is any from https://github.com/bpostlethwaite/colormap
We have to be able to render multiple lines with different colors. There is no such problem for scatter plot, since we can define marker/color/border per-point.
[{a}, {b}, {c}]
or array as property {x: [a, b, c], y: [a, b, c], z: shared}
? We never used array as options before and that is not a pattern [yet], also it does not allow for shared
properties or we have to detect them manually.Unfortunately due to uncertain regl/webgl issue regl-project/regl#432 we can't use offset with instanced draw, hence we have to pick scatter strategy: tracking elements/textures and rendering subset of them.
UPD: Turns out offset/elements in instanced draw affects uninstanced part, which gets repeated, not the instances, ie. line segment lineEnd
and lineTop
in our case. Therefore we have to rebind attributes or at least their offsets for every polyline instance. And seems that is is not difficult: we can set offset
a function. Yay!
Consider
let regl = require('regl')({extensions: 'angle_instanced_arrays'})
let line2d = require('./')(regl)
const arr1 = [0, 1, 1, NaN, 2, 1, 3, 2]
const arr2 = [0, 3, 1, NaN, 2, 3, 3, 3]
const rng = [-1, -1, 4, 4]
const vp = [80, 80, 556, 350]
line2d.render({ thickness: 4, points: arr1, color: 'red', viewport: vp, range: rng, join: 'rect', overlay: true })
line2d.render({ thickness: 4, points: arr2, color: 'blue', viewport: vp, range: rng, join: 'rect', overlay: true })
which results in
where the gap in the blue trace at the 2nd (x,y) position is missing.
plotly.js example in -> plotly/plotly.js#3551
join
, no cap
, no overlay
Also that is a pattern, to have 3 types of miters.
For really simple and fast shaders we skip complex lines.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.