canvasity
DUB package is a port of the C++ library canvas_ity.h
as seen here.
It provides a canvas type Canvasity
similar to HTML Canvas API.
- Familiar Canvas API
- CSS color Support through the
colors
DUB package. - Lines, quadratic and bezier paths.
- Line joins, line caps.
fill()
ANDstroke()
.- Support
shadowBlur
. - Support
setLineDash
. - Support
lineWidth
. - Support
save
/restore
properly. - Support
clip
paths. - Support
globalAlpha
and mostglobalCompositeOperation
modes. - Trapezoidal anti-aliasing.
- Gamma-aware rect blending, interpolation, and resampling.
- Premultiplied blending.
- Support various underlying buffer types, 1 to 4 channels:
- sRGB 8-bit
- sRGB 16-bit
- sRGB 32-bit float
- Quality options with
CanvasOptions
. - Suitable for
-betterC
,nothrow @nogc
. - Amortized allocations, all buffers are reused.
- SIMD optimizations. But for more performance for filled paths, consider using
dplug:canvas
. - Stringly typed constants, but also enums for performance.
- Restore support for gradients
- Restore and solidify support for fonts
- Restore dither
- Restore image pattern fill
- Display-P3 support
The library does no input or output on its own. Instead, you must provide it with buffers to copy into or out of.
This buffer must be a gamut
Image
.
import canvasity;
import gamut;
void main() {
Image image;
image.create(250, 250, PixelType.rgba8);
Canvasity canvas = Canvasity(image);
canvas.fillStyle = "#fff";
canvas.fillRect(0, 0, 250, 250);
canvas.fillStyle("red");
canvas.fillRect(140, 20, 40, 250);
canvas.fillStyle("blue");
canvas.fillRect(50, 50, 150, 100);
image.saveToFile("output-rectangle.png");
}
This example illustrate how .fill()
and .stroke
may be used in any order.
import canvasity;
import gamut;
void main() {
Image image;
image.create(300, 150, PixelType.rgba16);
with(Canvasity(image)) {
lineWidth = 30;
strokeStyle = "red";
lineJoin = "round";
// Stroke on top of fill
beginPath;
rect(25, 25, 100, 100);
fill;
stroke;
// Fill on top of stroke
beginPath;
rect(175, 25, 100, 100);
stroke;
fill;
}
image.convertTo8Bit();
image.saveToFile("output-shadow.png");
}
This example adds a blurred shadow to a rectangle. The shadowColor
property sets its color, and shadowBlur
sets its level of blurriness.
import canvasity;
import gamut;
void main() {
Image image;
image.create(300, 300, PixelType.rgba16);
with(Canvasity(image)) {
shadowBlur = 20;
shadowOffsetX = 10;
shadowOffsetY = 10;
shadowColor("rgba(0, 0, 0, 0.5)");
fillStyle("purple");
fillRect(60, 60, 190, 190);
}
image.saveToFile("output-shadow.png");
}