Skip to content

Commit 8840ccf

Browse files
committed
docs: Add programmatical usage example
1 parent 7c10a55 commit 8840ccf

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ For other than React-based web project, you can still use XML data and compose i
4343

4444
Embedding XML into HTML is now possible for CompoDrawJS. See [the example](https://github.com/Thor-x86/compodraw-js/blob/dev/examples/usage-with-dom.md).
4545

46+
## Use only with JavaScript
47+
48+
For logic-based composition, you can compose Instructs directly without `composeWith*` function. See [the example](https://github.com/Thor-x86/compodraw-js/blob/dev/examples/usage-programmatical.md).
49+
4650
## How to Contribute
4751

4852
You are free to contribute by opening an issue or pull-request. But before pull-request, it's better to test your changes locally with this command:

examples/usage-programmatical.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Example with Programmatical Method
2+
3+
**NOTE:** In this example, we're assuming you are using [compodraw-instructs](https://github.com/Thor-x86/compodraw-js-instructs).
4+
5+
This method is suitable for logic-based composition. On this example, we don't use any framework. However, you can implement this on any framework that currently you're using.
6+
7+
There is 3 files in a single folder:
8+
- `index.html` -> Your HTML file
9+
- `compodraw.js` -> CompoDraw bundled javascript file -- [download](https://github.com/Thor-x86/compodraw-js/releases)
10+
- `compodraw-instructs.js` -> bundled instructs package -- [download](https://github.com/Thor-x86/compodraw-js-instructs/releases)
11+
12+
Then write this snippet code to `index.html`:
13+
14+
```html
15+
<!DOCTYPE html>
16+
<html>
17+
<head>
18+
<title>CompoDraw Example with XML</title>
19+
<style>
20+
html, body, canvas {
21+
width: 100vw;
22+
height: 100vh;
23+
margin: 0;
24+
padding: 0;
25+
overflow: hidden;
26+
}
27+
28+
* {
29+
box-sizing: border-box;
30+
}
31+
</style>
32+
</head>
33+
<body>
34+
<canvas id="viewport">
35+
<strong>Canvas is NOT supported!</strong>
36+
</canvas>
37+
38+
<!-- Load the bundled JavaScripts -->
39+
<!-- make sure they are in the same folder -->
40+
<script src="compodraw.js"></script>
41+
<script src="compodraw-instructs.js"></script>
42+
43+
<script>
44+
// If instructs package name contains
45+
// dash character "-", make an alias for it
46+
const instructs = self['compodraw-instructs'].all;
47+
48+
// Compose logically
49+
const composed = new instructs.Elevate();
50+
for(let i = 0; i < 10; i++) {
51+
const color = i * 255 / 10;
52+
const rectangle = new instructs.Rectangle();
53+
rectangle.x = 20 + (120 * i);
54+
rectangle.y = 20;
55+
rectangle.color = `rgba(${color},${color},${color},1)`;
56+
composed.content.push(rectangle);
57+
}
58+
59+
// Get the canvas element
60+
const element = document.getElementById('viewport');
61+
62+
// Redraw on browser window resize,
63+
// otherwise it will stretches
64+
window.onresize = () => {
65+
compodraw.draw(composed, element);
66+
};
67+
68+
// Now, let's draw the canvas!
69+
compodraw.draw(composed, element);
70+
</script>
71+
72+
</body>
73+
</html>
74+
```

0 commit comments

Comments
 (0)