8 |
9 |
10 | キャンバスを介して Canvas ソースを処理するための純粋な JavaScript ライブラリ。
11 |
12 |
13 | [中文文档](./README.zh.md) | [English](./README.md) | [日本語](./README.jp.md)
14 |
15 | [examples](http://events.jackpu.com/daycaca/)
16 |
17 |
18 | ## 使い方
19 |
20 | ### Npm
21 |
22 | ``` bash
23 | $ npm install daycaca --save
24 | ```
25 |
26 | ``` js
27 | // es6import daycaca from 'daycaca';// src specify an image src (url or base64)
28 | daycaca.rotate(src, degress, (data, w, h) => {
29 | // your code here
30 | });
31 | ```
32 |
33 | ### CDN (ブラウザーで直接使う)
34 |
35 | ``` html
36 |
37 |
42 | ```
43 |
44 | ## API
45 |
46 | すべてのAPI source は以下の1つのタイプでなければなりません:
47 | + 画像url (URLを使う場合、クロスドメインの設置にお気を付けください。)
48 | + 画像のDOM節点
49 | + [type="file"]を使用する画像ファイルの対象值
50 |
51 | ### base64(source, callback)
52 |
53 | 画像をbase64 code値に転換する。参考数elは画像のDOM節点やDOMのURLである。
54 |
55 | ``` js
56 | const img = document.querySelector('img')
57 | daycaca.base64(img, (data) => {
58 | //... handle base64
59 | })
60 | ```
61 |
62 | ### compress(source, quailty, callback)
63 |
64 | 画像を圧縮する場合、画質を圧縮し、画像のサイズを小さくする。
65 |
66 | + PNGは無損失圧縮されるので、quality無効。
67 | + JPG/JPEG/BMP は損失圧縮される。
68 |
69 | `quality` は圧縮後の画質を表す。Qualityの数値が大きいほど、画質が高いのである。
70 |
71 | ``` js
72 | const img = document.querySelector('img')daycaca.compress(img, 0.5,(data) => {
73 | //... handle base64
74 | })
75 | ```
76 |
77 | ### crop(source, option, callback)
78 |
79 | 画像をカットする。
80 |
81 | `option` {} では、指定する必要がある参考数値である:
82 |
83 | + `x`;はカットされたエリアが画像の左までの距離
84 | + `y`;はカットされたエリアが画像の上までの距離
85 | + `w`;はカットされたエリアの横幅
86 | + `h`;はカットされたエリアの高度
87 | + `ratio`: スケール比率;
88 |
89 | ``` js
90 | const img = document.querySelector('img')
91 | daycaca.reszie(img, {
92 | x: 10,
93 | y: 20,
94 | w: 100,
95 | h: 70
96 | },(data) => {
97 | //... handle base64
98 | })
99 | ```
100 |
101 | ### rotate(source, degree, callback)
102 |
103 | 画像を回転させる。
104 |
105 | ``` js
106 | const img = document.querySelector('img')
107 | daycaca.rotate(img, 90,(data) => {
108 | //... handle base64
109 | })
110 | ```
111 |
112 | ### reszie(source, ratio, callback)
113 |
114 | 画像を拡大、縮小させる;
115 |
116 | + `ratio` (0~1)は画像のスケール比率である。1と設定する場合は、画像の大きさが変わらない。0以下の数値は設定できない。
117 |
118 | ``` js
119 | const img = document.querySelector('img')
120 | daycaca.reszie(img, 0.5,(data) => {
121 | //... handle base64
122 | })
123 | ```
124 |
125 | ### コメント
126 |
127 | ご意見やご質問大歓迎です!
128 |
129 |
130 |
131 |
132 |
133 |
--------------------------------------------------------------------------------
/README.zh.md:
--------------------------------------------------------------------------------
1 | # daycaca
2 |
3 | [](https://circleci.com/gh/JackPu/daycaca)
4 | [](https://www.npmjs.com/package/daycaca)
5 |
6 |
7 |
8 |
9 | 一款基于 canvas 图片处理类库,它可以帮助你处理图片的压缩,裁剪等;
10 |
11 | [中文文档](./README.zh.md) | [English](./README.md) | [日本語](./README.jp.md)
12 |
13 | [examples](http://events.jackpu.com/daycaca/)
14 |
15 |
16 | ## 快速开始
17 |
18 | ### npm
19 |
20 | ``` bash
21 | $ npm install daycaca -save
22 | ```
23 |
24 |
25 | ``` es6
26 | // es6
27 | import daycaca from 'daycaca';
28 | // src specify an image src (url or base64)
29 | daycaca.rotate(src, degress, (data, w, h) => {
30 | // your code here
31 | });
32 |
33 | ```
34 |
35 | ### 直接在浏览器使用
36 |
37 | ``` js
38 |
39 |
40 |
46 | ```
47 |
48 |
49 |
50 | ## API
51 |
52 | 所有 api 中的 `source` 它可以是;
53 |
54 | + 图片 url 地址 (如果使用 url 注意对于 [跨域](https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image) 的设置)
55 | + 图片的 DOM 节点
56 | + 一个图片的 [文件对象](https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications) 使用 `input[type="file"]` 读取的对象值
57 |
58 | ### base64(source, callback)
59 |
60 | 将图片转换成 base64 code 值;参数 `el` 可以为一个图片的 Dom 节点也可以是一个图片地址;
61 |
62 | ``` js
63 | const img = document.querySelector('img')
64 | daycaca.base64(img, (data) => {
65 | //... handle base64
66 | })
67 | ```
68 |
69 | ### compress(source, quailty, callback)
70 |
71 | 压缩图片,会将图片进行质量上的压缩, 从而降低图片的大小。
72 |
73 | + PNG 走的无损压缩,类库参考 .因此 `quailty` 无效
74 |
75 | + JPG/JPEG/BMP 等位图走的有损压缩
76 |
77 | `quality` 表示图片压缩的质量,值越大,图片会清晰
78 |
79 |
80 | ``` js
81 | const img = document.querySelector('img')
82 | daycaca.compress(img, 0.5,(data) => {
83 | //... handle base64
84 | })
85 | ```
86 |
87 | ### crop(source, option, callback)
88 |
89 | 裁剪图片,将图片裁剪至指定大小。
90 |
91 | option {} 里面需要指定的参数:
92 |
93 | + x: 裁剪的区域距离图片的左边缘的距离
94 | + y: 裁剪的区域距离图片的上边缘的距离
95 | + w: 裁剪的区域的宽度
96 | + h: 裁剪的区域的高度
97 | + ratio 缩放比例
98 |
99 |
100 |
101 |
102 | ``` js
103 | const img = document.querySelector('img')
104 | daycaca.reszie(img, {
105 | x: 10,
106 | y: 20,
107 | w: 100,
108 | h: 70
109 | },(data) => {
110 | //... handle base64
111 | })
112 | ```
113 |
114 |
115 | ### rotate(source, degree, callback)
116 |
117 | 旋转图片至某个角度。
118 |
119 | ``` js
120 | const img = document.querySelector('img')
121 | daycaca.rotate(img, 90,(data) => {
122 | //... handle base64
123 | })
124 | ```
125 |
126 |
127 | ### reszie(source, ratio, callback)
128 |
129 | 图片缩放,将图片进行放大缩小
130 |
131 | + ratio 表示图片缩放的比例,其中 1 表示图片不进行缩放,最小值必须 **大于0**
132 |
133 | ``` js
134 | const img = document.querySelector('img')
135 | daycaca.reszie(img, 0.5,(data) => {
136 | //... handle base64
137 | })
138 | ```
139 |
140 | ## 贡献
141 |
142 | 欢迎您提出自己的代码 PR 以及任何建议 😄😄🌺🌺🎆🎆
143 |
144 |
145 | ## MIT License
146 |
147 |
148 |
149 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # daycaca
2 |
3 | [](https://circleci.com/gh/JackPu/daycaca)
4 | []()
5 | []()
6 |
7 |
8 |
9 |
10 | A pure JavaScript library to handle image source via canvas.
11 |
12 | [中文文档](./README.zh.md) | [English](./README.md) | [日本語](./README.jp.md)
13 |
14 | [examples](http://events.jackpu.com/daycaca/)
15 |
16 |
17 | ## How to use
18 |
19 | ### Npm
20 |
21 | ``` bash
22 | $ npm install daycaca -save
23 | ```
24 |
25 |
26 | ``` es6
27 | // es6
28 | import daycaca from 'daycaca';
29 | // src specify an image src (url or base64)
30 | daycaca.rotate(src, degress, (data, w, h) => {
31 | // your code here
32 | });
33 |
34 | ```
35 |
36 | ### CDN
37 |
38 | ``` js
39 |
40 |
41 |
47 | ```
48 |
49 |
50 |
51 | ## API
52 |
53 | All API methods's argument `source` should be one type below:
54 |
55 | + an image url (Pay attention to [CORS](https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image) settings)
56 | + an IMG element
57 | + [a file object](https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications) Which use `input[type="file"]` value as source
58 |
59 | ### base64(source, callback)
60 |
61 | Convert your image to base64.
62 |
63 | ``` js
64 | const img = document.querySelector('img')
65 | daycaca.base64(img, (data) => {
66 | //... handle base64
67 | })
68 | ```
69 |
70 | ### compress(source, quailty, callback)
71 |
72 | Compress your image and minify the size of your image.
73 |
74 | + PNG need lossless compression; So the param `quality` may not work.
75 |
76 | + JPG/JPEG/BMP need lossy compression;
77 |
78 | `quality` (1~100). 100 means that the image keeps the same quality.
79 |
80 |
81 | ``` js
82 | const img = document.querySelector('img')
83 | daycaca.compress(img, 0.5,(data) => {
84 | //... handle base64
85 | })
86 | ```
87 |
88 | ### crop(source, option, callback)
89 |
90 | Crop your image to the size which you specify.
91 |
92 | option {} :
93 |
94 | + x: The x-axis distance between the crop area and the image;
95 | + y: The y-axis distance between the crop area and the image;
96 | + w: The width of crop area;
97 | + h: The height of crop area
98 | + ratio: the scale ration of the image
99 | + fixedWidth: get the image with fixed width
100 | + fixedHieght: get the image with fixed height
101 |
102 |
103 |
104 |
105 | ``` js
106 | const img = document.querySelector('img')
107 | daycaca.reszie(img, {
108 | x: 10,
109 | y: 20,
110 | w: 100,
111 | h: 70
112 | },(data) => {
113 | //... handle base64
114 | })
115 | ```
116 |
117 | ### rotate(source, degree, callback)
118 |
119 | Rotate your image to any degree.
120 |
121 | ``` js
122 | const img = document.querySelector('img')
123 | daycaca.rotate(img, 90,(data) => {
124 | //... handle base64
125 | })
126 | ```
127 |
128 |
129 | ### reszie(source, ratio, callback)
130 |
131 | Scale the image;
132 | + ratio (0~1): the scale ratio of the image. 1 means the image keep the same size;
133 |
134 | ``` js
135 | const img = document.querySelector('img')
136 | daycaca.reszie(img, 0.5,(data) => {
137 | //... handle base64
138 | })
139 | ```
140 |
141 | ## Contributions
142 |
143 | Your contributions and suggestions are welcome 😄😄🌺🌺🎆🎆
144 |
145 | ## Contributors
146 |
147 | + @Annie Tanslations of Japanese documents;
148 |
149 | ## MIT License
150 |
151 |
152 |
153 |
154 |
--------------------------------------------------------------------------------
/examples/style.css:
--------------------------------------------------------------------------------
1 | *{
2 | margin:0;
3 | padding: 0;
4 | }
5 |
6 | body{
7 | font-size: 16px;
8 | font-family: Helvetica, 'Microsoft Yahei', SimHei, arial, sans-serif;
9 | background:#e3e3e3;
10 | }
11 | html,body{
12 | height: 100%;
13 | }
14 | .page{
15 | position: relative;
16 | width: 100%;
17 | }
18 | img{
19 | max-width: 100%;
20 | margin-bottom: 20px;
21 | }
22 | img + p{
23 | font-size: 13px;
24 | color: #ddd;
25 | text-align: center;
26 | }
27 | a{
28 | color: #f6e342;
29 | text-decoration: none;
30 | transition: all .2s ease;
31 | }
32 | a:hover{
33 | color: #f9ec82;
34 | }
35 | h1{
36 | text-align: center;
37 | margin-top: 0;
38 | padding-top: 120px;
39 | padding-bottom: 40px;
40 | font-family: 'Monoton', cursive;
41 | font-size: 60px;
42 | letter-spacing: 2px;
43 | text-shadow: 0 6px 1px rgba(0,0,0,0.2);
44 | }
45 |
46 | h1 img{
47 | width: 480px;
48 |
49 | }
50 | h2{
51 | color: #333;
52 | padding-top: 60px;
53 | padding-bottom: 30px;
54 | text-align: center;
55 | }
56 | ul{
57 | padding-left: 40px;
58 | }
59 | code{
60 | word-break: break-all;
61 | }
62 | .js-navbar{
63 | display: none;
64 | }
65 | .js-navbar.active{
66 | display: block;
67 | background-color: rgba(255, 255, 255, .95);
68 | }
69 | .doc-hd{
70 | min-height: 75vh;
71 | background: #2d323c url(http://img1.vued.vanthink.cn/vued16831dfcbc402a474725d39217612578.jpeg) center bottom;
72 | background-size: cover;
73 | color:#f6e342;
74 | text-align: center;
75 | }
76 | .doc-hd p {
77 | font-size: 20px;
78 | color: #888;
79 | padding: 20px 0;
80 | }
81 | .btn-install{
82 | padding: 12px 36px;
83 | border-radius: 50px;
84 | font-size: 18px;
85 | color: #f6e342;
86 | background: #2d323c;
87 | border:2px solid #f6e342;
88 | }
89 | .btn-install:hover{
90 | color:#fff;
91 | border-color:#fff;;
92 | }
93 | .doc .btn{
94 | background-color: #f6e342;
95 | color: #333;
96 | border-color: #f6e342;
97 | border-radius: 20px;
98 | }
99 | .doc .btn:hover{
100 | box-shadow: 0 2px 4px rgba(0,0,0, .25);
101 | }
102 | .contents{
103 | padding: 20px;
104 | background: #fff;
105 | box-shadow: 0 6px 10px rgba(0,0,0,.15);
106 | max-width: 720px;
107 | margin: 0 auto 40px auto;
108 | }
109 |
110 | .doc-ft{
111 | padding-top: 40px;
112 | padding-bottom: 30px;
113 | background: #2d323c;
114 | color:#555;
115 | font-size: 13px;
116 | text-align: center;
117 | }
118 | pre{
119 | border-width: 0px;
120 | }
121 | @media all and (max-width:960px) {
122 | h2,.doc,.img-wrap{
123 | flex-wrap: wrap;
124 | }
125 | .img-wrap .item{
126 | width: 100%'
127 | }
128 | .js-navbar.active{
129 | display: none;
130 | }
131 | }
132 | /** view doc https://highlightjs.org **/
133 | .hljs {
134 | display: block;
135 | overflow-x: auto;
136 | padding: 0.5em;
137 | background: #f7f7f7;
138 | }
139 | .hljs,
140 | .hljs-subst {
141 | color: #2c3e50;
142 | }
143 | .hljs-comment {
144 | color: #999;
145 | }
146 | .hljs-keyword,
147 | .hljs-attribute,
148 | .hljs-selector-tag,
149 | .hljs-meta-keyword,
150 | .hljs-doctag,
151 | .hljs-name {
152 | font-weight: bold;
153 | }
154 | .hljs-type,
155 | .hljs-string,
156 | .hljs-number,
157 | .hljs-selector-id,
158 | .hljs-selector-class,
159 | .hljs-quote,
160 | .hljs-template-tag,
161 | .hljs-deletion {
162 | color: #e74c3c;
163 | }
164 | .hljs-title,
165 | .hljs-section {
166 | color: #e74c3c;
167 | font-weight: bold;
168 | }
169 | .hljs-regexp,
170 | .hljs-symbol,
171 | .hljs-variable,
172 | .hljs-template-variable,
173 | .hljs-link,
174 | .hljs-selector-attr,
175 | .hljs-selector-pseudo {
176 | color: #bc6060;
177 | }
178 | .hljs-literal {
179 | color: #78a960;
180 | }
181 | .hljs-built_in,
182 | .hljs-bullet,
183 | .hljs-code,
184 | .hljs-addition {
185 | color: #16a085;
186 | }
187 | .hljs-meta {
188 | color: #2980b9;
189 | }
190 | .hljs-meta-string {
191 | color: #2980b9;
192 | }
193 | .hljs-emphasis {
194 | font-style: italic;
195 | }
196 | .hljs-strong {
197 | font-weight: bold;
198 | }
199 |
200 | .navbar-default .navbar-nav>.active>a, .navbar-default .navbar-nav>.active>a:focus, .navbar-default .navbar-nav>.active>a:hover{
201 | background: transparent;
202 | color: #f6e342;
203 | }
204 | .btn{
205 | border-radius: 0;
206 | transition: all .2s ease-in;
207 | padding: 8px 30px;
208 | }
209 |
--------------------------------------------------------------------------------
/dist/daycaca.min.js:
--------------------------------------------------------------------------------
1 | var daycaca=function(t){function e(a){if(r[a])return r[a].exports;var i=r[a]={i:a,l:!1,exports:{}};return t[a].call(i.exports,i,i.exports,e),i.l=!0,i.exports}var r={};return e.m=t,e.c=r,e.d=function(t,r,a){e.o(t,r)||Object.defineProperty(t,r,{configurable:!1,enumerable:!0,get:a})},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,r){"use strict";var a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},i=Object.assign||function(t){for(var e=1;eA pure JavaScript library to handle image via canvas
37 |38 | Install Now 39 |
40 |Browser
44 |<script src="./dist/daycaca.min.js"></script>
45 | Npm
46 |$ npm install daycaca --save
47 | In your JS File
48 |daycaca.setConfig({});
49 |
50 | daycaca.base64('./images/test-base64.png', () => {
57 | //...
58 | });
59 | 60 | 61 |
62 |
65 | before
66 | 67 | 68 |daycaca.base64('./images/test-compress.jpg', 10, () => {
69 | //...
70 | });
71 | 72 | 73 |
74 |
76 | 裁剪前
77 | 78 | 79 |daycaca.crop('./images/test-compress.jpg', {
80 | x: 20,
81 | y: 20,
82 | w: 120,
83 | h: 60
84 | }, () => {
85 | //...
86 | });
87 | 88 | 89 |
90 |
92 | before
93 | 94 | 95 |daycaca.rotate(el, 90, () => {
96 | //...
97 | });
98 | 99 | 100 |
101 |
103 | before
104 | 105 | 106 |daycaca.resize(el, 2, () => {
107 | //...
108 | });
109 | 110 | 111 |
112 |