UNPKG

3.13 kBMarkdownView Raw
1# daycaca
2
3[![CircleCI](https://circleci.com/gh/JackPu/daycaca.svg?style=shield)](https://circleci.com/gh/JackPu/daycaca)
4[![npm](https://img.shields.io/npm/v/daycaca.svg?maxAge=2592000)]()
5
6
7<img src="http://img1.vued.vanthink.cn/vuede494856de5f2390a5727a6d98d488305.png" width="400">
8
9A pure JavaScript library to handle image source via canvas.
10
11[中文文档](./README.zh.md) | [English](./README.md) | [日本語](./README.jp.md)
12
13[examples](http://events.jackpu.com/daycaca/)
14
15
16## How to use
17
18### Npm
19
20``` bash
21$ npm install daycaca -save
22```
23
24
25``` es6
26// es6
27import daycaca from 'daycaca';
28// src specify an image src (url or base64)
29daycaca.rotate(src, degress, (data, w, h) => {
30 // your code here
31});
32
33```
34
35### CDN
36
37``` js
38<script src="./dist/daycaca.min.js"></script>
39
40<script>
41 // src specify an image src (url or base64)
42 daycaca.rotate(src, degress, (data, w, h) => {
43 // your code here
44 });
45</script>
46```
47
48
49
50## API
51
52All API methods's argument `source` should be one type below:
53
54+ an image url (Pay attention to [CORS](https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image) settings)
55+ an IMG element
56+ [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
57
58### base64(source, callback)
59
60Convert your image to base64.
61
62``` js
63const img = document.querySelector('img')
64daycaca.base64(img, (data) => {
65 //... handle base64
66})
67```
68
69### compress(source, quailty, callback)
70
71Compress your image and minify the size of your image.
72
73+ PNG need lossless compression; So the param `quality` may not work.
74
75+ JPG/JPEG/BMP need lossy compression;
76
77`quality` (1~100). 100 means that the image keeps the same quality.
78
79
80``` js
81const img = document.querySelector('img')
82daycaca.compress(img, 0.5,(data) => {
83 //... handle base64
84})
85```
86
87### crop(source, option, callback)
88
89Crop your image to the size which you specify.
90
91option {} :
92
93+ x: The x-axis distance between the crop area and the image;
94+ y: The y-axis distance between the crop area and the image;
95+ w: The width of crop area;
96+ h: The height of crop area
97+ ratio: the scale ration of the image
98+ fixedWidth: get the image with fixed width
99+ fixedHieght: get the image with fixed height
100
101<img width="480" src="http://img1.vued.vanthink.cn/vued233e94bd60775c0999df05d17b4642a8.png" />
102
103
104``` js
105const img = document.querySelector('img')
106daycaca.reszie(img, {
107 x: 10,
108 y: 20,
109 w: 100,
110 h: 70
111},(data) => {
112 //... handle base64
113})
114```
115
116### rotate(source, degree, callback)
117
118Rotate your image to any degree.
119
120``` js
121const img = document.querySelector('img')
122daycaca.rotate(img, 90,(data) => {
123 //... handle base64
124})
125```
126
127
128### reszie(source, ratio, callback)
129
130Scale the image;
131+ ratio (0~1): the scale ratio of the image. 1 means the image keep the same size;
132
133``` js
134const img = document.querySelector('img')
135daycaca.reszie(img, 0.5,(data) => {
136 //... handle base64
137})
138```
139
140## Contributions
141
142Your contributions and suggestions are welcome 😄😄🌺🌺🎆🎆
143
144## Contributors
145
146+ @Annie Tanslations of Japanese documents;
147
148## MIT License
149
150
151
152