|
| 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