Brushjs๋ HTML5์ canvas ์๋ฆฌ๋จผํธ๋ฅผ ํตํด 2D๊ทธ๋ํฝ ์์ ์ ์์ํ๊ฒ ๋ง๋ค์ด ์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ ๋๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก canvas์๋ฆฌ๋จผํธ์ ์ขํ๊ณ๋ ์ค๊ฒ ์ฐ๋ฆฌ๊ฐ ์ดํดํ๋ ์ขํ๊ณ์๋ ๋ค๋ฅธ ํํ์ ๋๋ค. ์ด๋ ๋ณต์กํ ๊ทธ๋ํฝ ์์ ์ ํ๋๋ฐ ์์ด์ ์ง๊ด์ ์ด์ง ์์ ํํ์ด๊ธฐ์ ์ด๋ฅผ ์ฐ๋ฆฌ๊ธฐ ์ดํด๊ฐ๊ธฐ ์ฌ์ด ํํ๋ก Brushjs๋ ๋ด๋ถ์์ ๋ง์ถฐ์ฃผ๋ ์์ ์ ํฉ๋๋ค.
๊ธฐ์กด HTML5 Canvas ์ขํ๊ณ
Brushjs๊ฐ ์ ๊ณตํ๋ ์ขํ๊ณ
Brushjs๋ canvas๋ผ๋ ํ๋์ ์๋ฆฌ๋จผํธ๋ฅผ ๊ตฌ์ฑํ๋ ๊ฐ๊ฐ์ ๊ทธ๋ํฝ ์์๋ค์ ๊ณ ์ ํ id๊ฐ์ ๊ธฐ๋ฐ์ผ๋ก ๊ด๋ฆฌ ๋๊ณ ์์ต๋๋ค. ์ด๋ ๋ง์น DOM ๊ฐ์ฒด๋ฅผ ๋ค๋ฃจ๋ ๋ฐฉ์๊ณผ ์ ์ฌํ๊ฒ ๋ค์ํ ๊ทธ๋ํฝ ํจ๊ณผ๋ ์ ๋๋ฉ์ด์ ํจ๊ณผ๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<canvas id="canvas" width="600" height="300"></canvas>
<script type="text/javascript" src="Brush.js"></script>
<script type="text/javascript">
var canvas = new Brush('canvas');
// Your code
</script>
</body>
</html>
#### - ๊ธฐ๋ณธ ๋ฉ์๋
##### set(element_info) set ๋ฉ์๋์ ์ธ์๋ก ์ ๋ฌ๋ ์ ๋ณด๋ new ๋ฅผ ํตํด ์์ฑ๋ ์ธ์คํด์ค์ ๋ ์ด์ด ์ ๋ณด๋ก ์ถ๊ฐ๊ฐ ๋ฉ๋๋ค. ```javascript var circle = { ... info } canvas.set(circle)
<br/>
##### draw()
set์ ํตํด ์ ์ฅ๋ ๋ ์ด์ด๋ฅผ canvas์์ ๊ทธ๋ฆฌ๋ ์ญํ์ ํฉ๋๋ค. draw ๋ฉ์๋๊ฐ ํธ์ถ๋๊ธฐ ์ ์๋ set์ผ๋ก ์ ์ฅํ ๋ ์ด์ด๋ฅผ ๊ทธ๋ฆฌ์ง๋ ์์ต๋๋ค.
```javascript
canvas.draw()
##### get(layer_id) get ๋ฉ์๋๋ set์ ํตํด ์ ์ฅ๋ ๋ ์ด์ด์ ๊ณ ์ ์์ด๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํด๋น ๋ ์ด์ด ์ ๋ณด๋ฅผ ์ ๋ฌ๋ฐ์ต๋๋ค. ```javascript
canvas.get(id)
<br/>
##### getLayers()
getLayers ๋ฉ์๋๋ set์ ํตํด ์ ์ฅ๋ ์ ์ฒด ๋ ์ด์ด ์ ๋ณด๋ฅผ ์ ๋ฌ๋ฐ์ต๋๋ค.
```javascript
canvas.getLayers()
##### delete(layer_id) delete ๋ฉ์๋๋ set์ ํตํด ์ ์ฅ๋ ๋ ์ด์ด ์ ๋ณด๋ฅผ ์ญ์ ํฉ๋๋ค. ```javascript
canvas.delete(id)
<br/>
##### clear()
clear ๋ฉ์๋๋ ํด๋น canvas ์์ญ์ ๋ชจ๋ ์ง์๋๋ค.
```javascript
canvas.clear()
##### animate(elements_info) animate ๋ฉ์๋๋ ์ ๋๋ฉ์ด์ ํจ๊ณผ๋ฅผ ์ค ๋ ์ด์ด ์ ๋ณด๋ฅผ ์ค์ ํฉ๋๋ค. ```javascript var moving = { // moving animation } canvas.animate(moving)
<br/>
##### deleteAnimation(layer_id)
deleteAnimation ๋ฉ์๋๋ animate ๋ฉ์๋๋ฅผ ํตํด ๋ฑ๋ก๋ ์๋๋ฉ์ด์
์ ๋ณด๋ฅผ ์ ๊ฑฐํฉ๋๋ค.
```javascript
canvas.deleteAnimation(id)
##### start() animate ๋ฉ์๋๋ฅผ ํตํด ๋ฑ๋ก๋ ์๋๋ฉ์ด์ ์ ๋ณด๋ก ์ ๋๋ฉ์ด์ ์ ์์ํฉ๋๋ค. ```javascript
canvas.start()
<br/><br/>
#### - ์ด๋ฒคํธ ๋ฉ์๋
<br/>
##### click(callback)
ํด๋ฆญ์ ํ์ํ ํ๋์ callback ํจ์๋ก ์ ๋ฌํฉ๋๋ค.
```javascript
var callbackFn = function(){
//....
}
canvas.click(callbackFn)
##### mousemove(callback) canvas ์์์ ๋ง์ฐ์ค๋ฅผ ์ด๋ ์ํฌ๋ ํ์ํ ํ๋์ callback ํจ์๋ก ์ ๋ฌํฉ๋๋ค. ```javascript var callbackFn = function(){ //.... }
canvas.mousemove(callbackFn)
<br/><br/>
### 4.๊ธฐ๋ณธ ๊ทธ๋ํฝ (Non-Animation)
<br/>
#### Line
-
```javascript
var line = {
id : ๊ณ ์ ํ ID ๊ฐ(string),
type: 'line',
points : [
[x0, y0], // ํ์ [int, int]
[x1, y1], // ํ์ [int, int]
...
[xn, yn]
],
lineWidth : ๋ผ์ธ ๋๊ป(string),
strokeStyle : ๋ผ์ธ ์นผ๋ฌ(string),
lineCap : ๋ผ์ธ์ ๋ ๋ง๋ฌด๋ฆฌ ํํ,
lineJoin : ๋ผ์ธ๊ฐ์ ์ฐ๊ฒฐ ํํ, //miter , round, bevel
isClose : ๋์ ๊ณผ ์์์ ์ฐ๊ฒฐ ์ ๋ฌด (bool),
isFill : isClose ๊ฐ true์ธ ๊ฒฝ์ฐ์ ๋ด๋ถ ์ ์ฑ์ (bool),
fillStyle : isFill ์ด true์ธ ๊ฒฝ์ฐ ๋ด๋ถ ์ (string),
opacity : ํฌ๋ช
๋ (0 < opacity < 1)
};
#### ์์ ```javascript var line = { id : 'line1', type: 'line', from : [0, 100], points : [ [100, 200], [200, 180], [300, 300], [400, 200], [600, 0] ], lineWidth : 1, strokeStyle : 'gray', lineCap : 'round', lineJoin : 'round', isClose : false, isFill : false, fillStyle : 'green', opacity : 0.5 } canvas.set([ line ]).draw(); ```
#### MutiLine -
var MutiLine = {
id : ๊ณ ์ ํ ID ๊ฐ(string),
type: 'mutiLine',
type: 'line',
points : [
[x0, y0], // ํ์ [int, int]
[x1, y1], // ํ์ [int, int]
...
[xn, yn]
],
lineWidth : ๋ผ์ธ ๋๊ป(string),
strokeStyle : ๋ผ์ธ ์นผ๋ฌ(string),
lineCap : ๋ผ์ธ์ ๋ ๋ง๋ฌด๋ฆฌ ํํ,
lineJoin : ๋ผ์ธ๊ฐ์ ์ฐ๊ฒฐ ํํ, //miter , round, bevel
isClose : ๋์ ๊ณผ ์์์ ์ฐ๊ฒฐ ์ ๋ฌด (bool),
isFill : isClose ๊ฐ true์ธ ๊ฒฝ์ฐ์ ๋ด๋ถ ์ ์ฑ์ (bool),
fillStyle : isFill ์ด true์ธ ๊ฒฝ์ฐ ๋ด๋ถ ์ (string),
opacity : ํฌ๋ช
๋ (0 < opacity < 1)
};
#### ์์ ```javascript var multiLine = { id: 'multiLine', type: 'mutiLine', from: [0, 100], lines : [ { type : 'line', points: [ [10, 100] ] }, { type : 'quadraticCurve', points : [ [110, 118, 210, 100] ] }, { type : 'line', points: [ [230, 100] ] }, { type : 'quadraticCurve', points : [ [330, 118, 430, 100] ] }, { type : 'line', points: [ [430, 120], [0 ,120] ] },
], lineWidth : 3, strokeStyle : '#000', lineCap : 'round', lineJoin : 'round', isFill : false, fillStyle: 'red' } canvas.set([ multiLine ]).draw();
<br/>
#### Circle
-
```javascript
var circle = {
id : ๊ณ ์ ํ ID ๊ฐ(string),
type: 'circle',
points : [
[x1 , y1 , radius1],
...
[x , y , radius]
]
startAngle : ์์ ๊ฐ (int),
endAngle : ์ข
๋ฃ ๊ฐ (int),
radius : ๋ฐ์ง๋ฆ(int),
lineWidth : ๋ผ์ธ ๋๊ป(string),
strokeStyle : ๋ผ์ธ ์นผ๋ฌ(string),
isFill : ๋ด๋ถ ์ ์ฑ์(bool),
fillStyle : isFill์ด true์ธ ๊ฒฝ์ฐ ๋ด๋ถ ์(string),
opacity : ํฌ๋ช
๋ (0 < opacity < 1)
}
#### ์์ ```javascript var circle = { id : 'ball1', type: 'circle', points : [ [100, 100 , 20] ], startAngle : 0, endAngle : Math.PI*2, radius : 20, lineWidth : 1, strokeStyle : 'red', isFill : true, fillStyle : 'red' } canvas.set([ circle ]).draw(); ```
#### Rectangle - ```javascript var rectangle ={ id : ๊ณ ์ ํ ID ๊ฐ(string), type: 'rectangle', points : [ [x1, y1, width1, height1], ... [x, y, width, height] ], lineWidth : ๋ผ์ธ ๋๊ป(string), strokeStyle : ๋ผ์ธ ์นผ๋ฌ(string), isFill : ๋ด๋ถ ์ ์ฑ์(bool), fillStyle : isFill์ด true์ธ ๊ฒฝ์ฐ ๋ด๋ถ ์(string), opacity : ํฌ๋ช ๋ (0 < opacity < 1) } ```
#### ์์ ```javascript var rectangle = { id: 'rectangle', type: 'rectangle', points : [ [50, 50, 100, 100] ], isFill : true, fillStyle : 'red', lineWidth : 1, strokeStyle : 'gray', opacity : 1, fillStyle : 'red' } canvas.set([ rectangle ]).draw(); ```
#### Text -
var text = {
id : ๊ณ ์ ํ ID ๊ฐ(string),
type: 'text',
points : [
[x , y],
...
],
text : ํ
์คํธ (string),
font : ํฐํธ ์์ฑ (string),
textAlign : ํ
์คํธ ์ ๋ ฌ,
textBaseline : ํ
์คํธ ๋ฒ ์ด์ค ๋ผ์ธ ํํ,
fillStyle : ํฐํธ ์ (string),
lineWidth : ํฐํธ ๋ผ์ธ ๋๊นจ(string),
strokeStyle : ํฐํธ ๋ผ์ธ ์ (string),
opacity : ํฌ๋ช
๋ (0 < opacity < 1)
}
#### ์์
var text ={
id : 'text1',
type: 'text',
font : '38pt Arial',
fillStyle : 'yellow',
lineWidth : 1,
//strokeStyle : 'blue',
text : 'Hello World',
textAlign : 'center',
textBaseline : 'middle',
points : [
[200 , 50],
[300 , 150]
]
}
canvas.set([
text
]).draw();
#### Cubic Bรฉzier curve(BezierCurve) -
var bezierCurve = {
id : ๊ณ ์ ํ ID ๊ฐ(string),
type: 'bezierCurve',
from : [x, y], // ์์์ (int)
points : [
[controlePointX1, controlePointY1, controlePointX2, controlePointY2, endPointX1, endPointY2]
...
],
lineWidth : ํฐํธ ๋ผ์ธ ๋๊นจ(string),
strokeStyle : ํฐํธ ๋ผ์ธ ์ (string),
isClose : ๋์ ๊ณผ ์์์ ์ฐ๊ฒฐ ์ ๋ฌด (bool),
isFill : isClose ๊ฐ true์ธ ๊ฒฝ์ฐ์ ๋ด๋ถ ์ ์ฑ์ (bool),
fillStyle : isFill ์ด true์ธ ๊ฒฝ์ฐ ๋ด๋ถ ์ (string),
opacity : ํฌ๋ช
๋ (0 < opacity < 1)
}
#### ์์ 
var bezierCurve ={
id : 'bazier1',
type: 'bezierCurve',
from : [20, 20],
points : [
[20,100,200,100,200,20]
],
lineWidth : 3,
strokeStyle : 'green',
isClose : false,
isFill : false,
fillStyle : 'red'
}
canvas.set([
bezierCurve
]).draw();
#### Quadratic Bรฉzier curve(QuadraticCurve) -
var quadraticCurve ={
id : 'quadratic',
type: 'quadraticCurve',
from : [x, y], // ์์์ (int)
points : [
[controlePointX1, controlePointY1, endPointX1, endPointY1]
],
lineWidth : ํฐํธ ๋ผ์ธ ๋๊นจ(string),
strokeStyle : ํฐํธ ๋ผ์ธ ์ (string),
lineCap : ๋ผ์ธ์ ๋ ๋ง๋ฌด๋ฆฌ ํํ,
isFill : isClose ๊ฐ true์ธ ๊ฒฝ์ฐ์ ๋ด๋ถ ์ ์ฑ์ (bool),
opacity : ํฌ๋ช
๋ (0 < opacity < 1)
}
####์์
var quadraticCurve ={
id : 'quadratic',
type: 'quadraticCurve',
from : [20, 20],
points : [
[20, 100, 200, 20]
],
lineWidth : 10,
strokeStyle : 'blue',
lineCap : 'square',
isClose : false
}
canvas.set([
quadraticCurve
]).draw();
var arcTo = {
id : ๊ณ ์ ํ ID ๊ฐ(string),
type: 'arcTo',
from : [x, y], // ์์์ (int)
points : [
[x1, y1 ,x2, y2, radius]
],
lineWidth : ๋ผ์ธ ๋๊ป(string),
strokeStyle : ๋ผ์ธ ์นผ๋ฌ(string),
lineCap : ๋ผ์ธ์ ๋ ๋ง๋ฌด๋ฆฌ ํํ,
lineJoin : ๋ผ์ธ๊ฐ์ ์ฐ๊ฒฐ ํํ, //miter , round, bevel
isClose : ๋์ ๊ณผ ์์์ ์ฐ๊ฒฐ ์ ๋ฌด (bool),
isFill : isClose ๊ฐ true์ธ ๊ฒฝ์ฐ์ ๋ด๋ถ ์ ์ฑ์ (bool),
fillStyle : isFill ์ด true์ธ ๊ฒฝ์ฐ ๋ด๋ถ ์ (string),
opacity : ํฌ๋ช
๋ (0 < opacity < 1)
}
var arcTo = {
id : 'arc2',
type: 'arcTo',
from : [300, 200],
points : [
[350, 200 ,350, 250, 50]
],
lineWidth : 2,
strokeStyle : 'black',
lineCap : 'round',
lineJoin : 'round', //miter , round, bevel
isClose : false,
isFill : false
}
canvas.set([
arcTo
]).draw();
canvas.animate([
{
id: '๊ณ ์ ํ ID ๊ฐ(string),
type: 'stroking', // moving
speed: 1, // how fast
time: 1 // how many
}
]);
#### Stroking -
var stroke =
{
id : 'stroke1',
type: 'stroke',
points : [
[0, 100],
[100, 200],
[200, 180],
[300, 300],
[400, 200],
[600, 0]
],
lineWidth : 1,
strokeStyle : '#000',
lineCap : 'round',
lineJoin : 'round'
}
canvas.set([
stroke
]).draw().animate([
{
id: 'stroke1',
type: 'stroking',
speed: 1,
time: 1
}
]).start();
#### Moving -
var multiLine = {
id: 'multiLine',
type: 'mutiLine',
from: [0, 100],
lines : [
{
type : 'line',
points: [
[10, 100]
]
},
{
type : 'quadraticCurve',
points : [
[110, 118, 210, 100]
]
},
{
type : 'line',
points: [
[230, 100]
]
}
],
lineWidth : 3,
strokeStyle : '#000',
lineCap : 'round',
lineJoin : 'round',
isFill : false,
fillStyle: 'red'
}
canvas.set([
multiLine
]).draw();
var circle = {
id : 'ball1',
type: 'circle',
points : [
[100, 100 , 20]
],
startAngle : 0,
endAngle : Math.PI*2,
radius : 20,
lineWidth : 1,
strokeStyle : 'red',
isFill : true,
fillStyle : 'red'
}
canvas.set([
circle,
multiLine
]).draw().animate([
{
id: 'ball1',
type: 'moving',
moveTo : [200, 200],
speed: 1,
time: 2
},
{
id: 'multiLine',
type: 'moving',
moveTo : [200, 200],
speed: 1,
time: 2
}
]).start();
#### Click ```javascript var circle = { id : 'ball1', type: 'circle', points : [ [100, 100 , 20] ], startAngle : 0, endAngle : Math.PI*2, radius : 20, lineWidth : 1, strokeStyle : 'red', isFill : true, fillStyle : 'red' }
var clickEvent = function(self , position){ canvas.get('ball1').points.forEach(function(elem){ elem[0] = position.x; elem[1] = position.y; self.draw() }) } canvas.set([ circle ]).draw().click(clickEvent);
<br/>
#### Mousemove
```javascript
var circle = {
id : 'ball1',
type: 'circle',
points : [
[100, 100 , 20]
],
startAngle : 0,
endAngle : Math.PI*2,
radius : 20,
lineWidth : 1,
strokeStyle : 'red',
isFill : true,
fillStyle : 'red'
}
var clickEvent = function(self , position){
canvas.get('ball1').points.forEach(function(elem){
elem[0] = position.x;
elem[1] = position.y;
self.draw()
})
}
canvas.set([
circle
]).draw().mousemove(clickEvent);
Rectangle(updated 2016.07.13)Gradient(effect)(updated 2016.07.15)Shadow(effect)(updated 2016.07.15)globalCompositeOperation(updated 2016.07.19)- Scale(animation)
- Rotate(animation)
moving to specific points (animation)(updated 2016.09.01)- Image
- Performance
- Document in English
- Brushjs Gallery (updating)