Skip to content

Commit f17fc6d

Browse files
fix with complex nine patch image
1 parent 7b4bce4 commit f17fc6d

File tree

1 file changed

+16
-54
lines changed

1 file changed

+16
-54
lines changed

README.md

+16-54
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,40 @@
22
Scale nine-patch image using JavaScript's canvas.
33

44
## 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-
105
* 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.
168

179
## Example
1810

1911
```js
20-
const srcImg = 'https://res.cloudinary.com/drcrre4xg/image/upload/v1516665655/test_normal.9_gjksbl.png';
12+
const srcImg = 'test_normal.9.png';
2113
const WIDTH = 150;
2214
const HEIGHT = 150;
2315

24-
var ninePatch;
16+
document.addEventListener("DOMContentLoaded", event => {
17+
let $ = document.querySelector.bind(document);
2518

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));
4422

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));
5726

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+
});
7031

7132
function setImage(divElement, srcURL, width, height) {
7233
divElement.style.width = width + 'px';
7334
divElement.style.height = height + 'px';
7435
divElement.style.backgroundSize = '' + width + 'px ' + height + 'px';
7536
divElement.style.backgroundImage = "url('" + srcURL + "')";
7637
}
38+
7739
```
7840
## References
7941

0 commit comments

Comments
 (0)