Hey guys,
im currently developing a plugin for Adobe XD.
At some point the extension will access an image uploaded, and convert it to a pixel representation for further processing.
For this code out of the npm package get-image-pixel is used, shown below:
/****************************** Code Block *********************************/**
module.exports = function(image, opts) {
opts = opts || {}
opts.x = opts.x || 0
opts.y = opts.y || 0
opts.width = typeof opts.width === 'number' ? opts.width : image.width
opts.height = typeof opts.height === 'number' ? opts.height : image.height
if (!context) {
canvas = document.createElement("canvas")
context = canvas.getContext('2d')
}
canvas.width = opts.width
canvas.height = opts.height
context.clearRect(0, 0, opts.width, opts.height)
context.drawImage(image, opts.x, opts.y, opts.width, opts.height, 0, 0, opts.width, opts.height)
var imgData
try {
imgData = context.getImageData(0, 0, opts.width, opts.height)
}
catch(e) {
module.exports.dispose()
throw e
}
return imgData.data
}
/****************************** Code Block *********************************/
The problem i’m facing now is when trying to get the context of the created canvas (indicated by the little arrow in the Code Block).
The thrown problem is:
Plugin TypeError: canvas.getContext is not a function
Interestingly enough the created canvas is a owner document of the following form rather than a canvas element:
My guess is that it has something to do with the adobe xd environment and the creating of the canvas element is not working as
it would in a browser.
My guess is that it has something to do the t
{ _ownerDocument:
{ _ownerDocument: [Circular],
_selectorEngine:
{ _document: [Circular],
_styleSheetList: {},
_styleNodes: {},
_styleSheets: [],
_styleNodesMutation: 0,
_linkNodesMutation: 0,
_cachedNodeList: null,
_cachedEnclosingClassNames: {},
.....
Do you have any idea how to solve this problem or even another way to get the pixel representation of an image? You would really help me out!