Files
cocos4/jsb/builtin/jsb-adapter/HTMLCanvasElement.js
T
James Chen 2d7e3ade55 Reverts HTMLCanvasElement change in PR #2684. (#2700)
* Reverts HTMLCanvasElement change in PR #2684.
It's a bug of alignment value while reading texture. Adds _alignment variable in HTMLCanvasElement.

* Default value of width& height of Canvas is 0.

* Call PixelStorei before texImage2D in C++.
2018-05-21 09:54:36 +08:00

160 lines
4.8 KiB
JavaScript

/****************************************************************************
Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
const HTMLElement = require('./HTMLElement');
const ImageData = require('./ImageData');
const DOMRect = require('./DOMRect');
class CanvasGradient {
constructor() {
console.log("==> CanvasGradient constructor");
}
addColorStop(offset, color) {
console.log("==> CanvasGradient addColorStop");
}
}
class TextMetrics {
constructor(width) {
this._width = width;
}
get width() {
return this._width;
}
}
class HTMLCanvasElement extends HTMLElement {
constructor(width, height) {
super('canvas')
this.id = 'glcanvas';
this.type = 'canvas';
this.top = 0;
this.left = 0;
this._width = width ? Math.ceil(width) : 0;
this._height = height ? Math.ceil(height) : 0;
this._context2D = null;
this._data = null;
this._alignment = 4; // Canvas is used for rendering text only and we make sure the data format is RGBA.
}
//TODO: implement opts.
getContext(name, opts) {
var self = this;
// console.log(`==> Canvas getContext(${name})`);
if (name === 'webgl' || name === 'experimental-webgl') {
return window.__ccgl;
} else if (name === '2d') {
if (!this._context2D) {
this._context2D = new CanvasRenderingContext2D(this._width, this._height);
this._context2D._canvas = this;
this._context2D._setCanvasBufferUpdatedCallback(function(data) {
// FIXME: Canvas's data will take 2x memory size, one in C++, another is obtained by Uint8Array here.
self._data = new ImageData(data, self._width, self._height);
// If the width of canvas could be divided by 2, it means that the bytes per row could be divided by 8.
self._alignment = self._width % 2 === 0 ? 8 : 4;
});
}
return this._context2D;
}
return null;
}
toDataURL() {
//TODO:
console.log("==> Canvas toDataURL");
return "";
}
set width(width) {
width = Math.ceil(width);
if (this._width !== width) {
this._width = width;
if (this._context2D) {
this._context2D._width = width;
}
}
}
get width() {
return this._width;
}
set height(height) {
height = Math.ceil(height);
if (this._height !== height) {
this._height = height;
if (this._context2D) {
this._context2D._height = height;
}
}
}
get height() {
return this._height;
}
get clientWidth() {
return this._width;
}
get clientHeight() {
return this._height;
}
getBoundingClientRect() {
return new DOMRect(0, 0, this._width, this._height);
}
}
var ctx2DProto = CanvasRenderingContext2D.prototype;
ctx2DProto.createImageData = function(width, height) {
return new ImageData(width, height);
}
ctx2DProto.putImageData = function(imagedata, dx, dy) {
this._canvas._data = imagedata; //TODO: consider dx, dy?
}
ctx2DProto.getImageData = function(sx, sy, sw, sh) {
//TODO:cjh
return this._canvas._data;
}
ctx2DProto.drawImage = function(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) {
//TODO:cjh
}
//TODO:cjh
ctx2DProto.bezierCurveTo = function() {}
ctx2DProto.fill = function() {}
module.exports = HTMLCanvasElement;