A simple library that renders an isometric water box on a canvas
waterbox-canvas is a TypeScript/JavaScript library for rendering a water level visualization on an HTML5 Canvas or OffscreenCanvas.
Check out the demo on the project page or head over to StackBlitz to fiddle around.
npm install waterbox-canvas
yarn add waterbox-canvas
pnpm add waterbox-canvas
There is a build specifically for the browser:
<script src="dist/waterbox-canvas.browser.js"></script>
Create a canvas element:
<canvas id="my-canvas"></canvas>
Create Waterbox:
import { createWaterbox } from 'waterbox-canvas';
const canvas = document.getElementById('my-canvas') as HTMLCanvasElement;
// Create a waterbox instance
const waterbox = createWaterbox(canvas)
.width(80)
.height(120);
// Set the water level (0-100)
waterbox.value(50);
// Render the waterbox
waterbox.render();
import { createWaterbox } from 'waterbox-canvas';
// Initialize waterbox
const canvas = document.getElementById('waterbox') as HTMLCanvasElement;
const waterbox = createWaterbox(canvas)
.width(80)
.height(120)
.padding(5)
.tiltAngle(30)
.backColorScheme({
fill: 'rgba(192,192,224,1.0)',
stroke: 'rgba(127,127,192,1.0)',
contrast: 0.1,
})
.waterColorScheme({
fill: 'rgba(64,64,224,0.6)',
stroke: 'rgba(64,64,224,0.6)',
contrast: 0.1,
})
.scale({
divisions: 5,
size: 0.2,
})
.strokeWidths({
inner: 1,
outer: 3,
});
waterbox.value(20).render();
createWaterbox(canvas: HTMLCanvasElement | OffscreenCanvas): WaterboxCreates a new waterbox instance for the given canvas.
Parameters:
canvas - The HTML canvas or OffscreenCanvas element to render toReturns: A Waterbox instance
Waterbox propertieswidthwidth(): numberwidth(value: number): WaterboxGet or set the width of the canvas.
heightheight(): numberheight(value: number): WaterboxGet or set the height of the canvas.
paddingpadding(): numberpadding(value: number): WaterboxGet or set the padding of the drawn waterbox.
valuevalue(): numbervalue(value: number): WaterboxGet or set the water fill level.
tiltAngletiltAngle(): number | undefinedtiltAngle(value?: number): WaterboxGet or set the isometric tilt angle
backColorSchemebackColorScheme(): ColorSchemebackColorScheme(value: ColorScheme): WaterboxGet or set the color scheme of the backside of the rendered waterbox.
waterbox.backColorScheme({
fill: 'rgba(80, 80, 111, 1)',
stroke: 'rgba(80, 80, 111, 1)',
contrast: 0.1
});
waterColorSchemewaterColorScheme(): ColorSchemewaterColorScheme(value: ColorScheme): WaterboxGet or set the color scheme of the water rendered inside the waterbox.
waterbox.waterColorScheme({
fill: 'rgba(58, 123, 213, 0.9)',
stroke: 'rgba(42, 92, 160, 0.9)',
lighter: 'rgba(90, 149, 224, 0.9)',
darker: 'rgba(43, 95, 168, 0.9)',
});
frontColorSchemefrontColorScheme(): ColorScheme | undefinedfrontColorScheme(value?: ColorScheme): WaterboxGet or set the color scheme of the front of the rendered waterbox.
waterbox.frontColorScheme({
fill: 'rgba(255, 255, 255, 0.1)',
innerStroke: 'rgba(255, 255, 255, 0.05)',
outerStroke: 'rgba(255, 255, 255, 0.5)',
contrast: 0.1,
});
backPatternbackPattern(): Pattern | undefinedbackPattern(value?: Pattern): WaterboxGet or set the pattern drawn on top of the backside of the waterbox.
waterbox.backPattern({
name: "grid",
size: 15,
alpha: 1.0
});
waterPatternwaterPattern(): Pattern | undefinedwaterPattern(value?: Pattern): WaterboxGet or set the pattern drawn on top of the water.
waterbox.waterPattern({
creator: (ctx) => {
// render pattern
return ctx.createPattern(patternCanvas, 'repeat');
}
});
frontPatternfrontPattern(): Pattern | undefinedfrontPattern(value?: Pattern): WaterboxGet or set the pattern drawn on top of the front of the waterbox.
strokeWidthsstrokeWidths(): StrokeWidthsstrokeWidths(value: StrokeWidths): WaterboxGet or set the stroke inner and outer widths in pixels.
waterbox.strokeWidths({
inner: 1,
outer: 2
});
scalescale(): Scale | undefinedscale(value?: Scale): WaterboxGet or set the scale drawn in the waterbox.
waterbox.scale({
size: 0.2,
divisions: 5,
position: 'back',
});
clipEdgesclipEdges(): booleanclipEdges(value: boolean): WaterboxGet or set whether edges are clipped from the drawn waterbox. If edges are clipped, they appear as transparent.
optionsoptions(): Optionsoptions(value: Partial<Options>): WaterboxGet or set multiple options at once.
MIT