4.1.139 • Published 10 days ago

@thi.ng/hdom-canvas v4.1.139

Weekly downloads
295
License
Apache-2.0
Repository
github
Last release
10 days ago

hdom-canvas

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

@thi.ng/hdom component wrapper for declarative canvas scenegraphs.

This package provides a re-usable canvas component, which accepts child nodes defining a scene tree of different shape types in standard @thi.ng/hiccup syntax/format (i.e. nested arrays) and then translates these into canvas API draw calls during the hdom update process / cycle.

Status

STABLE - used in production

Search or submit any issues for this package

BREAKING CHANGES 3.0.0

The actual tree traversal & drawing has been extracted to the new @thi.ng/hiccup-canvas package for better re-usability, also outside without hdom.

Related packages

Installation

yarn add @thi.ng/hdom-canvas

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/hdom-canvas"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const hdomCanvas = await import("@thi.ng/hdom-canvas");

Package sizes (gzipped, pre-treeshake): ESM: 919 bytes

Dependencies

Usage examples

Several demos in this repo's /examples directory are using this package.

A selection:

ScreenshotDescriptionLive demoSource
Interactive inverse FFT toy synthDemoSource
Convex hull & shape clipping of 2D polygonsDemoSource
Doodle w/ K-nearest neighbor search result visualizationDemoSource
Poisson-disk shape-aware sampling, Voronoi & Minimum Spanning Tree visualizationDemoSource
Realtime analog clock demoDemoSource
Interactive pattern drawing demo using transducersDemoSource
2D Bezier curve-guided particle systemDemoSource
Various hdom-canvas shape drawing examples & SVG conversion / exportDemoSource
Canvas based Immediate Mode GUI componentsDemoSource
Minimal IMGUI usage exampleDemoSource
Animated sine plasma effect visualized using contour linesDemoSource
Basic rstream-gestures multi-touch demoDemoSource
Unison wavetable synth with waveform editorDemoSource
Animated Voronoi diagram, cubic splines & SVG downloadDemoSource
2D scenegraph & shape pickingDemoSource
2D scenegraph & image map based geometry manipulationDemoSource
Fork-join worker-based raymarch renderer (JS/CPU only)DemoSource

API

Generated API docs

import { start } from "@thi.ng/hdom";
import { canvas } from "@thi.ng/hdom-canvas";

start(() => {
    const t = Date.now() * 0.001;
    return [canvas, { width: 100, height: 100 },
        ["circle", { fill: "red", stroke: "black" }, [50, 50], 25 + 25 * Math.sin(t)]
    ];
});

Usage with @thi.ng/geom shape primitives:

import { start } from "@thi.ng/hdom";
import { canvas } from "@thi.ng/hdom-canvas";
import * as g from "@thi.ng/geom";

start(() => {
    const t = Date.now() * 0.001;
    return [canvas, { width: 100, height: 100 },
        g.group(
            { translate: [50, 50], fill: "none" },
            [
                g.withAttribs(
                    g.asPolygon(g.circle(50), 6),
                    { rotate: t % Math.PI, stroke: "red" }
                ),
                g.star(25 + 25 * Math.sin(t), 6, [0.5, 1], { stroke: "blue" }),
            ]
        )
    ];
});

How it works

The package provides a canvas component which uses the branch-local behavior implementation feature of @thi.ng/hdom v5.0.0 to support virtual SVG-like shape elements / components. These are defined as part of the main UI component tree just like any other component, but are then translated into canvas API draw commands during the hdom update process. Any embedded shape component functions receive the user context object as first arg, just like normal hdom components.

Shape components are expressed in standard hiccup syntax (or as objects implementing the IToHiccup() interface, like the shape types provided by @thi.ng/geom), and with the following...

Restrictions & behavior controls

  • Shape component objects with life cycle methods are only partially supported, i.e. only the render & release methods are used.
  • For performance reasons release methods are disabled by default. If your shape tree contains stateful components which use the release life cycle method, you'll need to explicitly enable the canvas component's __release control attribute by setting it to true.
  • Currently no event listeners can be assigned to shapes (ignored), though this is planned for a future version. The canvas element itself can of course have event handlers as usual.

For best performance it's recommended to ensure all resulting shapes elements are provided in already normalized hiccup format, i.e.

[tag, {attribs}, ...] // or
[tag, null, ...]

That way the __normalize: false control attribute can be added either to the canvas component itself (or to individual shapes / groups), and if present, will skip normalization of that element's children.

Likewise, for animated scenes, the __diff control attribute should be set to false to skip unnecessary diffing and force redraws.

To disable the automatic background clearing of the canvas, set the __clear attribute to false.

[canvas, { width: 100, height: 100, __clear: false }, ...]

HDPI support

The canvas component automatically adjusts its size for HDPI displays by adding CSS width & height properties and pre-scaling the drawing context accordingly before any shapes are processed. For fullscreen canvases simply set the width & height attribs to:

[canvas,
    {
        width: window.innerWidth,
        height: window.innerHeight
    },
    // shapes
    ...
]

SVG conversion

Even though the element names & syntax are very similar to SVG elements, for performance reasons all geometry data given to each shape remains un-stringified (only styling attributes are). However, the @thi.ng/hiccup-svg package provides a convertTree() function which takes the arguably more "raw" shape format used by hdom-canvas and converts an entire shape tree into SVG compatible & serializable format. Note: the tree MUST first be normalized (if not already) using hdom-canvas' normalizeTree().

import { serialize } from "@thi.ng/hiccup";
import { convertTree, svg } from "@thi.ng/hiccup-svg";
import { normalizeTree } from "@thi.ng/hdom-canvas";

serialize(
    svg({ width: 100, height: 100},
        convertTree(
            normalizeTree(
                {}, // default normalization options
                ["g",
                    {
                        fill: "red",
                        stroke: "none",
                        translate: [50, 50]
                    },
                    ["circle", {}, [0, 0], 25],
                    ["polygon", { fill: "white" },
                        [[-10,10],[10,10],[0,-10]]
                    ]
                ]
            )
        )
    )
);
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="100">
    <g transform="translate(50.00 50.00)" fill="red" stroke="none">
        <circle cx="0.00" cy="0.00" r="25.00"/>
        <polygon points="-10.00,10.00 10.00,10.00 0.00,-10.00" fill="white"/>
    </g>
</svg>

Supported shape types

Please see the @thi.ng/hiccup-canvas README for the full list of supported shapes, gradients, attributes, colors and transformations.

Authors

Maintainer

Contributors

If this project contributes to an academic publication, please cite it as:

@misc{thing-hdom-canvas,
  title = "@thi.ng/hdom-canvas",
  author = "Karsten Schmidt and others",
  note = "https://thi.ng/hdom-canvas",
  year = 2018
}

License

© 2018 - 2021 Karsten Schmidt // Apache Software License 2.0

4.1.139

10 days ago

4.1.138

13 days ago

4.1.137

15 days ago

4.1.135

27 days ago

4.1.136

25 days ago

4.1.134

1 month ago

4.1.133

1 month ago

4.1.132

1 month ago

4.1.131

1 month ago

4.1.130

2 months ago

4.1.129

2 months ago

4.1.128

2 months ago

4.1.127

2 months ago

4.1.126

2 months ago

4.1.125

2 months ago

4.1.124

2 months ago

4.1.123

2 months ago

4.1.122

2 months ago

4.1.121

2 months ago

4.1.120

2 months ago

4.1.117

2 months ago

4.1.118

2 months ago

4.1.119

2 months ago

4.1.115

3 months ago

4.1.116

3 months ago

4.1.114

3 months ago

4.1.113

3 months ago

4.1.112

3 months ago

4.1.110

3 months ago

4.1.111

3 months ago

4.1.109

3 months ago

4.1.107

3 months ago

4.1.106

3 months ago

4.1.104

4 months ago

4.1.105

4 months ago

4.1.103

4 months ago

4.1.102

5 months ago

4.1.100

5 months ago

4.1.101

5 months ago

4.1.99

5 months ago

4.1.97

5 months ago

4.1.98

5 months ago

4.1.96

5 months ago

4.1.95

5 months ago

4.1.74

8 months ago

4.1.75

8 months ago

4.1.76

8 months ago

4.1.77

8 months ago

4.1.70

9 months ago

4.1.71

9 months ago

4.1.72

9 months ago

4.1.73

8 months ago

4.1.78

8 months ago

4.1.79

8 months ago

4.1.64

10 months ago

4.1.65

9 months ago

4.1.67

9 months ago

4.1.68

9 months ago

4.1.69

9 months ago

4.1.90

6 months ago

4.1.92

6 months ago

4.1.93

6 months ago

4.1.94

5 months ago

4.1.80

7 months ago

4.1.85

7 months ago

4.1.86

6 months ago

4.1.87

6 months ago

4.1.88

6 months ago

4.1.81

7 months ago

4.1.82

7 months ago

4.1.83

7 months ago

4.1.84

7 months ago

4.1.89

6 months ago

4.1.63

11 months ago

4.1.61

11 months ago

4.1.62

11 months ago

4.1.59

12 months ago

4.1.60

11 months ago

4.1.57

1 year ago

4.1.58

1 year ago

4.1.52

1 year ago

4.1.53

1 year ago

4.1.54

1 year ago

4.1.55

1 year ago

4.1.50

1 year ago

4.1.51

1 year ago

4.1.56

1 year ago

4.1.49

1 year ago

4.1.47

1 year ago

4.1.48

1 year ago

4.1.46

1 year ago

4.1.43

1 year ago

4.1.45

1 year ago

4.1.38

1 year ago

4.1.39

1 year ago

4.1.41

1 year ago

4.1.42

1 year ago

4.1.40

1 year ago

4.1.30

2 years ago

4.1.31

2 years ago

4.1.32

2 years ago

4.1.33

2 years ago

4.1.34

2 years ago

4.1.35

1 year ago

4.1.36

1 year ago

4.1.37

1 year ago

4.1.27

2 years ago

4.1.28

2 years ago

4.1.29

2 years ago

4.1.25

2 years ago

4.1.26

2 years ago

4.1.24

2 years ago

4.1.20

2 years ago

4.1.21

2 years ago

4.1.22

2 years ago

4.1.23

2 years ago

4.1.16

2 years ago

4.1.17

2 years ago

4.1.18

2 years ago

4.1.19

2 years ago

4.1.15

2 years ago

4.1.10

2 years ago

4.1.11

2 years ago

4.1.12

2 years ago

4.1.13

2 years ago

4.1.14

2 years ago

4.1.9

2 years ago

4.1.8

2 years ago

4.1.7

2 years ago

4.0.10

2 years ago

4.1.4

2 years ago

4.1.3

2 years ago

4.1.6

2 years ago

4.1.5

2 years ago

4.1.0

2 years ago

4.1.2

2 years ago

4.1.1

2 years ago

4.0.9

3 years ago

4.0.8

3 years ago

4.0.5

3 years ago

4.0.7

3 years ago

4.0.4

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

4.0.2

3 years ago

3.0.60

3 years ago

3.0.58

3 years ago

3.0.59

3 years ago

3.0.57

3 years ago

3.0.56

3 years ago

3.0.55

3 years ago

3.0.54

3 years ago

3.0.53

3 years ago

3.0.52

3 years ago

3.0.51

3 years ago

3.0.50

3 years ago

3.0.49

3 years ago

3.0.48

3 years ago

3.0.47

3 years ago

3.0.46

3 years ago

3.0.45

3 years ago

3.0.44

3 years ago

3.0.43

3 years ago

3.0.42

3 years ago

3.0.41

3 years ago

3.0.40

3 years ago

3.0.39

3 years ago

3.0.38

3 years ago

3.0.37

3 years ago

3.0.33

3 years ago

3.0.32

3 years ago

3.0.31

3 years ago

3.0.30

3 years ago

3.0.29

3 years ago

3.0.28

3 years ago

3.0.27

3 years ago

3.0.26

3 years ago

3.0.25

3 years ago

3.0.24

3 years ago

3.0.23

3 years ago

3.0.22

3 years ago

3.0.21

3 years ago

3.0.20

4 years ago

3.0.19

4 years ago

3.0.18

4 years ago

3.0.17

4 years ago

3.0.16

4 years ago

3.0.15

4 years ago

3.0.13

4 years ago

3.0.14

4 years ago

3.0.12

4 years ago

3.0.11

4 years ago

3.0.10

4 years ago

3.0.8

4 years ago

3.0.9

4 years ago

3.0.7

4 years ago

3.0.6

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.4.26

4 years ago

2.4.25

4 years ago

2.4.24

4 years ago

2.4.23

4 years ago

2.4.22

4 years ago

2.4.21

4 years ago

2.4.20

4 years ago

2.4.19

4 years ago

2.4.18

4 years ago

2.4.17

4 years ago

2.4.16

4 years ago

2.4.15

4 years ago

2.4.14

4 years ago

2.4.13

4 years ago

2.4.12

4 years ago

2.4.11

4 years ago

2.4.10

4 years ago

2.4.9

4 years ago

2.4.8

4 years ago

2.4.7

4 years ago

2.4.6

4 years ago

2.4.3

4 years ago

2.4.2

4 years ago

2.4.1

4 years ago

2.4.0

4 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.4

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.18

5 years ago

2.0.17

5 years ago

2.0.16

5 years ago

2.0.15

5 years ago

2.0.14

5 years ago

2.0.13

5 years ago

2.0.12

5 years ago

2.0.11

5 years ago

2.0.10

5 years ago

2.0.9

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.1.20

5 years ago

0.1.19

5 years ago

0.1.18

5 years ago

0.1.17

5 years ago

0.1.16

5 years ago

0.1.15

5 years ago

0.1.14

5 years ago

0.1.13

5 years ago

0.1.12

5 years ago

0.1.11

5 years ago

0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.1.0-alpha.1

6 years ago

0.1.0-alpha.0

6 years ago

0.1.0-alpha

6 years ago