bresenham-zingl
v0.1.1
Published
Bresenham rasterisation algorithms by Alois Zingl
Downloads
867
Readme
Bresenham rasterisation functions by Alois Zingl
Port of C code by Alois Zingl from this paper
Install
npm i -S bresenham-zingl
import { line, circle, quadBezier } from 'bresenham-zingl';
quadBezier(0,0, 10, 10, 0, 10, (x, y) => console.log(x, y)); // 0,0, ...
Demo
API
User-defined callbacks
setPixel(x, y)
Use that callback to fill the pixel on canvas.
Parameters
| parameter | type | description |
| --------- | ------ | ----------- |
| x
| number | |
| y
| number | |
setPixelAlpha(x, y, alpha)
Callback that would also receive the alpha value for the pixel
Parameters
| parameter | type | description |
| --------- | ------ | ----------- |
| x
| number | |
| y
| number | |
| alpha
| number | |
line(x0, y0, x1, y1, setPixel)
Line segment rasterisation
Parameters
| parameter | type | description |
| ---------- | -------- | ----------- |
| x0
| number | |
| y0
| number | |
| x1
| number | |
| y1
| number | |
| setPixel
| setPixel | |
lineAA(x0, y0, x1, y1, setPixelAA)
Draw a black (0) anti-aliased line on white (255) background
Parameters
| parameter | type | description |
| ------------ | ------------- | ----------- |
| x0
| number | |
| y0
| number | |
| x1
| number | |
| y1
| number | |
| setPixelAA
| setPixelAlpha | |
Returns number
,
lineWidth(x0, y0, x1, y1, wd, setPixel)
Plot an anti-aliased line of width wd
Parameters
| parameter | type | description |
| ---------- | -------- | ----------- |
| x0
| number | |
| y0
| number | |
| x1
| number | |
| y1
| number | |
| wd
| number | |
| setPixel
| setPixel | |
quadRationalBezierSegment(x0, y0, x1, y1, x2, y2, w, setPixel)
plot a limited rational Bezier segment, squared weight
Parameters
| parameter | type | description |
| ---------- | -------- | ----------- |
| x0
| number | |
| y0
| number | |
| x1
| number | |
| y1
| number | |
| x2
| number | |
| y2
| number | |
| w
| number | |
| setPixel
| setPixel | |
quadRationalBezierSegmentAA(x0, y0, x1, y1, x2, y2, w, setPixelAA)
draw an anti-aliased rational quadratic Bezier segment, squared weight
Parameters
| parameter | type | description |
| ------------ | ------------- | ----------- |
| x0
| number | |
| y0
| number | |
| x1
| number | |
| y1
| number | |
| x2
| number | |
| y2
| number | |
| w
| number | |
| setPixelAA
| setPixelAlpha | |
rotatedEllipse(x, y, a, b, angle, setPixel)
Plot ellipse rotated by angle (radian)
Parameters
| parameter | type | description |
| ---------- | -------- | ----------- |
| x
| number | |
| y
| number | |
| a
| number | |
| b
| number | |
| angle
| number | |
| setPixel
| setPixel | |
rotatedEllipseRect(x0, y0, x1, y1, zd, setPixel)
Rectangle encloMath.sing the ellipse, integer rotation angle
Parameters
| parameter | type | description |
| ---------- | -------- | ----------- |
| x0
| number | |
| y0
| number | |
| x1
| number | |
| y1
| number | |
| zd
| number | |
| setPixel
| setPixel | |
ellipseRect(x0, y0, x1, y1, setPixel)
Rectangular parameter encloMath.sing the ellipse
Parameters
| parameter | type | description |
| ---------- | -------- | ----------- |
| x0
| number | |
| y0
| number | |
| x1
| number | |
| y1
| number | |
| setPixel
| setPixel | |
circle(xm, ym, r, setPixel)
Circle rasterisation
Parameters
| parameter | type | description |
| ---------- | -------- | ----------- |
| xm
| number | |
| ym
| number | |
| r
| number | |
| setPixel
| setPixel | |
circleAA(xm, ym, r, setPixelAA)
Draw a black anti-aliased circle on white background
Parameters
| parameter | type | description |
| ------------ | ------------- | ----------- |
| xm
| number | |
| ym
| number | |
| r
| number | |
| setPixelAA
| setPixelAlpha | |
quadBezierSegment(x0, y0, x1, y1, x2, y2, setPixel)
plot a limited quadratic Bezier segment
Parameters
| parameter | type | description |
| ---------- | -------- | ----------- |
| x0
| number | |
| y0
| number | |
| x1
| number | |
| y1
| number | |
| x2
| number | |
| y2
| number | |
| setPixel
| setPixel | |
quadBezierAA(x0, y0, x1, y1, x2, y2, setPixelAA)
Plot any quadratic Bezier curve with anti-alias
Parameters
| parameter | type | description |
| ------------ | ------------- | ----------- |
| x0
| number | |
| y0
| number | |
| x1
| number | |
| y1
| number | |
| x2
| number | |
| y2
| number | |
| setPixelAA
| setPixelAlpha | |
quadBezierSegmentAA(x0, y0, x1, y1, x2, y2, setPixelAA)
Draw an limited anti-aliased quadratic Bezier segment
Parameters
| parameter | type | description |
| ------------ | ------------- | ----------- |
| x0
| number | |
| y0
| number | |
| x1
| number | |
| y1
| number | |
| x2
| number | |
| y2
| number | |
| setPixelAA
| setPixelAlpha | |
cubicBezier(x0, y0, x1, y1, x2, y2, x3, y3, setPixel)
plot any cubic Bezier curve
Parameters
| parameter | type | description |
| ---------- | -------- | ----------- |
| x0
| number | |
| y0
| number | |
| x1
| number | |
| y1
| number | |
| x2
| number | |
| y2
| number | |
| x3
| number | |
| y3
| number | |
| setPixel
| setPixel | |
cubicBezierAA(x0, y0, x1, y1, x2, y2, x3, y3, setPixelAA)
plot any cubic Bezier curve
Parameters
| parameter | type | description |
| ---------- | ------------- | ----------- |
| x0
| number | |
| y0
| number | |
| x1
| number | |
| y1
| number | |
| x2
| number | |
| y2
| number | |
| x3
| number | |
| y3
| number | |
| setPixel
| setPixelAlpha | |
License
(The MIT License)
Copyright (c) 2012 Alois Zingl
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.