|
2 | 2 | Scale nine-patch image using JavaScript's canvas. |
3 | 3 |
|
4 | 4 | ## Usage |
5 | | - * Constructor: NinePatch(srcImage, newWidth, newHeight) |
6 | | - + srcImage: (String) url of image. |
7 | | - + newWidth: (Number) new image's width |
8 | | - + newHeight: (Number) new image's height |
9 | | - |
10 | 5 | * APIs: |
11 | | - + setSrcImage(srcImage) |
12 | | - + setWidth(newWidth) |
13 | | - + setHeight(newHeight) |
14 | | - + getSize => {url, width, height}: 'url' is the original image's url |
15 | | - + run => {url, width, height}: 'url' is the new image's url, which is scaled. |
| 6 | + + getSize(srcImg) => {url, width, height}: 'url' is the original image's url |
| 7 | + + scaleImage(srcImg, newWidth, newHeight) => url: 'url' is the new image's url, which is scaled. |
16 | 8 |
|
17 | 9 | ## Example |
18 | 10 |
|
19 | 11 | ```js |
20 | | -const srcImg = 'https://res.cloudinary.com/drcrre4xg/image/upload/v1516665655/test_normal.9_gjksbl.png'; |
| 12 | +const srcImg = 'test_normal.9.png'; |
21 | 13 | const WIDTH = 150; |
22 | 14 | const HEIGHT = 150; |
23 | 15 |
|
24 | | -var ninePatch; |
| 16 | +document.addEventListener("DOMContentLoaded", event => { |
| 17 | + let $ = document.querySelector.bind(document); |
25 | 18 |
|
26 | | -window.onload = function() { |
27 | | - ninePatchWorker = new NinePatch(srcImg, WIDTH, HEIGHT); |
28 | | - view(); |
29 | | - normal(); |
30 | | - test(); |
31 | | -}; |
32 | | -/* |
33 | | -* Show nine patch image |
34 | | -*/ |
35 | | -function view() { |
36 | | - var ninePatchImgDiv = document.getElementById('ninePatchImg'); |
37 | | - ninePatchWorker |
38 | | - .getSize() |
39 | | - .then( |
40 | | - result => setImage(ninePatchImgDiv, result.url, result.width, result.height), |
41 | | - error => console.log('Get size of image error: ', error) |
42 | | - ); |
43 | | -} |
| 19 | + new NinePatch().getSize(srcImg) |
| 20 | + .then(result => setImage($('#ninePatchImg'), result.url, result.width, result.height)) |
| 21 | + .catch(error => console.log(error)); |
44 | 22 |
|
45 | | -/** |
46 | | - * Show image after scaling without handling nine-patch image |
47 | | - */ |
48 | | -function test() { |
49 | | - var testImgDiv = document.getElementById('testImg'); |
50 | | - ninePatchWorker |
51 | | - .getSize() |
52 | | - .then( |
53 | | - result => setImage(testImgDiv, result.url, result.width + 50, result.height + 100), |
54 | | - error => console.log('Get size of image error: ', error) |
55 | | - ); |
56 | | -} |
| 23 | + new NinePatch().scaleImage(srcImg, WIDTH, HEIGHT) |
| 24 | + .then(result => setImage($('#normalImg'), result, WIDTH, HEIGHT)) |
| 25 | + .catch(error => console.log(error)); |
57 | 26 |
|
58 | | -/* |
59 | | -* Show normal image after scaling nine-patch image |
60 | | -*/ |
61 | | -function normal() { |
62 | | - var normalImgDiv = document.getElementById('normalImg'); |
63 | | - ninePatchWorker |
64 | | - .run() |
65 | | - .then(result => setImage(normalImgDiv, result.url, result.width, result.height)) |
66 | | - .catch(error => { |
67 | | - console.log('Error: ', error); |
68 | | - }); |
69 | | -} |
| 27 | + new NinePatch().getSize(srcImg) |
| 28 | + .then(result => setImage($('#testImg'), result.url, result.width + 50, result.height + 100)) |
| 29 | + .catch(error => console.log(error)); |
| 30 | +}); |
70 | 31 |
|
71 | 32 | function setImage(divElement, srcURL, width, height) { |
72 | 33 | divElement.style.width = width + 'px'; |
73 | 34 | divElement.style.height = height + 'px'; |
74 | 35 | divElement.style.backgroundSize = '' + width + 'px ' + height + 'px'; |
75 | 36 | divElement.style.backgroundImage = "url('" + srcURL + "')"; |
76 | 37 | } |
| 38 | + |
77 | 39 | ``` |
78 | 40 | ## References |
79 | 41 |
|
|
0 commit comments