Skip to content

Commit 6b0ecd5

Browse files
committed
Initial commit
0 parents  commit 6b0ecd5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+18554
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*]
7+
indent_style = space
8+
indent_size = 2
9+
end_of_line = lf
10+
charset = utf-8
11+
trim_trailing_whitespace = true
12+
insert_final_newline = true
13+
14+
[*.{yml,yaml}]
15+
indent_size = 4

.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Template from: https://gist.github.com/salcode/10017553
2+
3+
# ignore all files starting with . or ~
4+
.*
5+
~*
6+
7+
# ignore node/grunt dependency directories
8+
node_modules/
9+
10+
# ignore build and types directory
11+
build/
12+
types/
13+
14+
# ignore OS generated files
15+
ehthumbs.db
16+
Thumbs.db
17+
18+
# ignore Editor files
19+
*.sublime-project
20+
*.sublime-workspace
21+
*.komodoproject
22+
23+
# ignore log files
24+
*.log
25+
26+
# ignore packaged files
27+
*.7z
28+
*.dmg
29+
*.gz
30+
*.iso
31+
*.jar
32+
*.rar
33+
*.tar
34+
*.zip
35+
36+
# track these files, if they exist
37+
!.gitignore
38+
!.editorconfig
39+
!.npmignore
40+
!.prettierignore

.npmignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Template from: https://gist.github.com/salcode/10017553
2+
3+
# ignore all files starting with . or ~
4+
.*
5+
~*
6+
7+
# ignore node/grunt dependency directories
8+
node_modules/
9+
10+
# ignore source directory
11+
src/
12+
13+
# ignore test directory
14+
tests/
15+
16+
# ignore example directory
17+
examples/
18+
19+
# ignore OS generated files
20+
ehthumbs.db
21+
Thumbs.db
22+
23+
# ignore Editor files
24+
*.sublime-project
25+
*.sublime-workspace
26+
*.komodoproject
27+
28+
# ignore log files
29+
*.log
30+
31+
# ignore packaged files
32+
*.7z
33+
*.dmg
34+
*.gz
35+
*.iso
36+
*.jar
37+
*.rar
38+
*.tar
39+
*.zip
40+
41+
# ignore configuration files
42+
webpack.config.json
43+
tsconfig.json
44+
45+
# track these files, if they exist
46+
!.npmignore

.prettierignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ignore node/grunt dependency directories
2+
node_modules/
3+
4+
# ignore build and types directory
5+
build/
6+
types/
7+
8+
# ignore self-tidy files
9+
tsconfig.json

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Athaariq Ardhiansyah
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# CompoDraw JS
2+
3+
**Compose & Draw for JavaScript**
4+
5+
Easiest way to **compose shapes and effects** with **XML** or **JSX** then **draw them** into **HTML5 Canvas**, without sacrifices **modularity** and **compatibility** among web frameworks.
6+
7+
CompoDraw uses multiple **instruct**s with each of them tells Canvas API how to draw specific thing like a shape or visual effect (e.g. Rotate, Move, Mask, ...). All you need to do is **just arrange them** by writing XML, or JSX if you are ReactJS user.
8+
9+
Instructs are bundled into one package, commonly wrapped with [UMD module](https://www.devguide.at/en/javascript/amd-cjs-umd-esm-modular-javascript/#Universal_Module_Definition_UMD) format. So you can import it to any web framework of your choice :)
10+
11+
## How to Install
12+
13+
1. Add to your project with Yarn:
14+
```bash
15+
yarn add compodraw
16+
```
17+
or with NPM:
18+
```bash
19+
npm install compodraw --save
20+
```
21+
or you can download the bundled JavaScript file for plain HTML or PHP project
22+
2. Then pick your preferred `instructs package`.
23+
For basic operation, use [compodraw-instructs](https://github.com/Thor-x86/compodraw-js-instructs). If you want, you can build your own instructs package with each instruct is a [class](https://www.w3schools.com/js/js_classes.asp) that has at least `draw(canvasContext)` method
24+
3. That's it! now you can use CompoDraw 😁
25+
26+
## How to Use
27+
28+
First of all, make sure your front-end project already initialized. You can choose either [React](https://reactjs.org/), [Vue](https://vuejs.org/), [Angular](https://angular.io/), [PHP](https://www.quora.com/Which-is-the-best-PHP-framework-for-beginners), or plain HTML. And then, choose your usage method:
29+
30+
### Use with React's JSX
31+
32+
With React, you are able to use the benefit of JSX. Here's [the example](https://github.com/Thor-x86/compodraw-js/blob/dev/examples/usage-with-jsx.md).
33+
34+
### Use with React's JSX (with Tree Shaking)
35+
36+
For production, we recommend you to follow [this example](https://github.com/Thor-x86/compodraw-js/blob/dev/examples/usage-with-jsx-treeshaking.md) instead of above.
37+
38+
### Use with XML
39+
40+
For other than React-based web project, you can still use XML data and compose it without parse by yourself. Here's [the example](https://github.com/Thor-x86/compodraw-js/blob/dev/examples/usage-with-xml.md).
41+
42+
### Use with Embedded XML inside HTML Tag
43+
44+
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).
45+
46+
## How to Contribute
47+
48+
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:
49+
50+
```bash
51+
yarn run test
52+
```
53+
54+
or with NPM:
55+
56+
```bash
57+
npm run test
58+
```
59+
60+
Then pull request after all of those tests passed.
61+
62+
**NOTE:** Test is not necessary if you only modify the docs or source code style
63+
64+
## Test on a Computer without GPU
65+
66+
If you are using VM like Hyper-V or Windows Subsystem for Linux (WSL), install [docker](https://docs.docker.com/docker-for-windows/install/#install-docker-desktop-on-windows) first. Then run this command:
67+
68+
```bash
69+
yarn run test:no-gpu
70+
```
71+
72+
or with NPM:
73+
74+
```bash
75+
npm run test:no-gpu
76+
```

examples/usage-with-dom.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Example with DOM
2+
3+
**NOTE:** In this example, we're assuming you are using [compodraw-instructs](https://github.com/Thor-x86/compodraw-js-instructs).
4+
5+
In some cases, we have to embed XML data into HTML. It's possible with CompoDraw! You just have to put XML data (as comment) into Canvas tag, or another HTML tag you like.
6+
7+
For this example, there is 3 files in a single folder:
8+
- `index.html` -> Your HTML file
9+
- `compodraw.js` -> CompoDraw bundled javascript file (Download link soon...)
10+
- `compodraw-instructs.js` -> bundled instructs package (Download link soon...)
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+
<!--
36+
<move x="100" y="125">
37+
<rectangle
38+
color="pink"
39+
x="10" width="200"
40+
y="20" height="150"
41+
/>
42+
<rectangle
43+
color="cyan"
44+
x="210" width="200"
45+
y="170" height="150"
46+
/>
47+
</move>
48+
-->
49+
</canvas>
50+
51+
<!-- Load the bundled JavaScripts -->
52+
<!-- make sure they are in the same folder -->
53+
<script src="compodraw.js"></script>
54+
<script src="compodraw-instructs.js"></script>
55+
56+
<script>
57+
// If instructs package name contains
58+
// dash character "-", make an alias for it
59+
const instructs = self['compodraw-instructs'].all;
60+
61+
// Get the canvas element
62+
const element = document.getElementById('viewport');
63+
64+
// Compose from canvas element inner text
65+
const composed = compodraw.composeFromXml(element, instructs);
66+
67+
// Redraw on browser window resize,
68+
// otherwise it will stretches
69+
window.onresize = () => {
70+
compodraw.draw(composed, element);
71+
};
72+
73+
// Now, let's draw the canvas!
74+
compodraw.draw(composed, element);
75+
</script>
76+
77+
</body>
78+
</html>
79+
```
80+
81+
**WARNING 1:** The XML **must** be commented. Why? that's because HTML will simply mess up the embedded XML data.
82+
83+
**WARNING 2:** If an element tag contains commented XML data, don't mix it with other HTML tag or text.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Usage with React JSX (with Tree Shaking Support)
2+
3+
**NOTE:** In this example, we're assuming you are using [compodraw-instructs](https://github.com/Thor-x86/compodraw-js-instructs).
4+
5+
Unlike other method, we can actually use the real benefit of JSX which allows us to do [Tree Shaking](https://developers.google.com/web/fundamentals/performance/optimizing-javascript/tree-shaking) for smaller output JS file.
6+
7+
Here's the simple code snippet for `src/App.js` to draw pink and cyan rectangle:
8+
9+
```jsx
10+
// Import dependencies:
11+
import React, {useEffect} from 'react';
12+
import {composeFromJsx, draw} from 'compodraw';
13+
14+
// Import CSS style, remove this
15+
// if you don't need it
16+
import './App.css';
17+
18+
// Import only required instructs:
19+
import {Move, Rectangle} from 'compodraw-instructs';
20+
21+
// Compose with JSX:
22+
const composed = composeFromJsx(
23+
<Move x={100} y={125}>
24+
<Rectangle
25+
color="pink"
26+
x={10} width={200}
27+
y={20} height={150}
28+
/>
29+
<Rectangle
30+
color="cyan"
31+
x={210} width={200}
32+
y={170} height={150}
33+
/>
34+
</Move>
35+
); // Notice that you don't have to
36+
///// insert the second parameter
37+
38+
// Create a function to handle draw
39+
// everytime browser need to do that
40+
function onDraw() {
41+
// Get the canvas element
42+
const element = document.getElementById('viewport');
43+
44+
// Making sure canvas is exist,
45+
// otherwise don't draw
46+
if(!element) return;
47+
48+
// Now, let's draw the canvas!
49+
draw(composed, element);
50+
}
51+
52+
// Your React App
53+
function App() {
54+
// Use hook to make sure canvas element is ready
55+
useEffect(() => {
56+
// Draw immediately after canvas is ready
57+
onDraw();
58+
59+
// Redraw on browser window resize,
60+
// otherwise it will stretches
61+
window.addEventListener('resize', onDraw);
62+
63+
// Remove the event when user
64+
// navigates to other page
65+
return () => window.removeEventListener('resize', onDraw);
66+
});
67+
68+
// Render canvas element:
69+
return (
70+
<canvas id="viewport">
71+
<strong>Canvas is NOT supported!</strong>
72+
</canvas>
73+
);
74+
}
75+
76+
// Don't forget this
77+
export default App;
78+
```
79+
80+
For experimental purpose, you can use this CSS snippet for `src/App.css` to make canvas fills entire webpage:
81+
82+
```css
83+
html, body, canvas {
84+
width: 100vw;
85+
height: 100vh;
86+
margin: 0;
87+
padding: 0;
88+
overflow: hidden;
89+
}
90+
91+
* {
92+
box-sizing: border-box;
93+
}
94+
```

0 commit comments

Comments
 (0)