“canvas1.getContext is not a function” error when creating a canvas element via XmlService

青春壹個敷衍的年華 提交于 2020-08-17 09:08:49

问题


I have been working on making projects inside Google Apps Script but came across a problem.

I am currently trying to get the individual pixels of an image, by using an HTML canvas(if you know of another way to get pixel data from an image in JS, please let me know). The problem is, Google Apps Script doesn't seem to have a getContext method for the canvas. I'm not sure if this is done purposely, or if it is some kind of bug.

Code:

var img = XmlService.createElement('img');
img.src = query;
var canvas1 = XmlService.createElement('canvas');
var context = canvas1.getContext('2d');

Error:

TypeError: canvas1.getContext is not a function (line 8, file "Code")

I am not sure how to fix or get around this.


回答1:


You created an element with: var canvas1 = XmlService.createElement('canvas');

These are the methods for an element




回答2:


The "canvas element" your are asking for is part of Canvas API.

Google Apps Script doesn't include functions for the canvas element, actually it doesn't include any function for any Web APIs elements.

The XMLService Service has the Class Element, this is

A representation of an XML Element node.



来源:https://stackoverflow.com/questions/63219204/canvas1-getcontext-is-not-a-function-error-when-creating-a-canvas-element-via

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!