Skip to content

Commit 4b3461a

Browse files
Merge pull request #708 from sketch-hq/image-size-property
ImageData size property
2 parents 0b69b6d + ba1f6c2 commit 4b3461a

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

CHANGELOG.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"unreleased": [
3-
"[New] Add `resizeToOriginalSize` method on `Image`"
3+
"[New] Add `resizeToOriginalSize` method on `Image`",
4+
"[New] Add `size` property on `ImageData`"
45
],
56
"releases": {
67
"63": ["[New] Updated `LibraryType` enum"],

Source/dom/models/ImageData.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ ImageData.define('nsdata', {
8080
},
8181
})
8282

83+
ImageData.define('size', {
84+
/**
85+
* The size of the image.
86+
*
87+
* @return {object} The image's size.
88+
*/
89+
get() {
90+
const s = this._object.image().size()
91+
const size = { width: parseFloat(s.width), height: parseFloat(s.height) }
92+
return size
93+
},
94+
})
95+
8396
ImageData.define('id', {
8497
exportable: true,
8598
importable: false,

Source/dom/models/__tests__/ImageData.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* globals expect, test */
22
import { base64Image } from '../../../test-utils'
3-
import { Image } from '../..'
3+
import { Image, Rectangle } from '../..'
44

55
test('should return an ImageData when accessing `image`', (context, document) => {
66
const page = document.selectedPage
@@ -13,3 +13,30 @@ test('should return an ImageData when accessing `image`', (context, document) =>
1313
})
1414
expect(image.image.type).toBe('ImageData')
1515
})
16+
17+
test('should return image size when accessing `size`', (context, document) => {
18+
const page = document.selectedPage
19+
20+
const image = new Image({
21+
parent: page,
22+
image: {
23+
base64: base64Image,
24+
},
25+
})
26+
expect(image.image.size.width).toBe(50)
27+
expect(image.image.size.height).toBe(50)
28+
})
29+
30+
test('should return image size regardless of scale', (context, document) => {
31+
const page = document.selectedPage
32+
33+
const image = new Image({
34+
parent: page,
35+
image: {
36+
base64: base64Image,
37+
},
38+
})
39+
image.frame = new Rectangle(0, 0, 300, 200)
40+
expect(image.image.size.width).toBe(50)
41+
expect(image.image.size.height).toBe(50)
42+
})

docs/api/Image.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,15 @@ For performance reasons, Sketch initializes the `Image` object lazily. So if you
7878
var imageData = imageLayer.image
7979

8080
imageData.nsimage // return a native NSImage
81-
image.nsdata // return a native NSData representation of the image
81+
imageData.nsdata // return a native NSData representation of the image
8282
```
8383

8484
An `ImageData` is a wrapper around a native `NSImage`.
8585

8686
You can access the native `NSImage` with `nsimage` or a native `NSData` representation of the image with `nsdata`.
87+
88+
```javascript
89+
imageLayer.image.size // { width: 100, height: 100 }
90+
```
91+
92+
As a convenience, you can access the original size of an `ImageData` object via its `size` property.

0 commit comments

Comments
 (0)