Compare commits
26 Commits
0.1.0-prer
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4afa7f5f8b | ||
|
|
6fb49c0f8b | ||
|
|
bce6246084 | ||
|
|
bb7229d22f | ||
|
|
d34510dcde | ||
|
|
aae7d29f0b | ||
|
|
e99ff82182 | ||
|
|
97a90ca09a | ||
|
|
998479973e | ||
|
|
16704af7d3 | ||
|
|
01c008ed11 | ||
|
|
e9ac9a23c2 | ||
|
|
65db4f201f | ||
|
|
786178f2ad | ||
|
|
01a4057fe3 | ||
|
|
df4fb26094 | ||
|
|
aaebd14ef3 | ||
|
|
b6ed521b16 | ||
|
|
9d64e582ea | ||
|
|
3b21ee98b6 | ||
|
|
7af5b3e207 | ||
|
|
e25b8946bc | ||
|
|
801b0dab1f | ||
|
|
438e0491b7 | ||
|
|
26fe38c0d8 | ||
|
|
5b36e937e2 |
1
.gitattributes
vendored
1
.gitattributes
vendored
@@ -14,6 +14,7 @@
|
|||||||
*.js text eol=lf
|
*.js text eol=lf
|
||||||
*.js.map text eol=lf
|
*.js.map text eol=lf
|
||||||
*.json text eol=lf
|
*.json text eol=lf
|
||||||
|
*.json5 text eol=lf
|
||||||
*.md text eol=lf
|
*.md text eol=lf
|
||||||
*.vert text eol=lf
|
*.vert text eol=lf
|
||||||
*.xml text eol=lf
|
*.xml text eol=lf
|
||||||
|
|||||||
@@ -19,10 +19,6 @@ cache:
|
|||||||
jobs:
|
jobs:
|
||||||
include:
|
include:
|
||||||
- stage: test
|
- stage: test
|
||||||
script:
|
|
||||||
- npm run lint
|
|
||||||
- npm run docs
|
|
||||||
- npm run tap
|
|
||||||
- stage: deploy
|
- stage: deploy
|
||||||
node_js: 10
|
node_js: 10
|
||||||
script: npm run build
|
script: npm run build
|
||||||
|
|||||||
44
package.json
44
package.json
@@ -24,26 +24,30 @@
|
|||||||
"watch": "webpack --progress --colors --watch --watch-poll"
|
"watch": "webpack --progress --colors --watch --watch-poll"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"babel-core": "^6.23.1",
|
"babel-core": "6.26.3",
|
||||||
"babel-eslint": "^10.1.0",
|
"babel-eslint": "10.1.0",
|
||||||
"babel-loader": "^7.1.4",
|
"babel-loader": "7.1.5",
|
||||||
"babel-polyfill": "^6.22.0",
|
"babel-polyfill": "6.26.0",
|
||||||
"babel-preset-env": "^1.6.1",
|
"babel-preset-env": "1.7.0",
|
||||||
"copy-webpack-plugin": "^4.5.1",
|
"copy-webpack-plugin": "4.6.0",
|
||||||
"docdash": "^0.4.0",
|
"docdash": "0.4.0",
|
||||||
"eslint": "^7.13.0",
|
"eslint": "7.31.0",
|
||||||
"eslint-config-scratch": "^6.0.0",
|
"eslint-config-scratch": "6.0.0",
|
||||||
"gh-pages": "^1.0.0",
|
"gh-pages": "1.2.0",
|
||||||
"jsdoc": "^3.6.0",
|
"jsdoc": "3.6.7",
|
||||||
"json": "^9.0.4",
|
"json": "9.0.6",
|
||||||
"playwright-chromium": "^1.0.1",
|
"playwright-chromium": "1.13.0",
|
||||||
|
"scratch-render-fonts": "1.0.0-prerelease.20210401210003",
|
||||||
"scratch-vm": "0.2.0-prerelease.20201125065300",
|
"scratch-vm": "0.2.0-prerelease.20201125065300",
|
||||||
"tap": "^11.0.0",
|
"tap": "11.1.5",
|
||||||
"travis-after-all": "^1.4.4",
|
"travis-after-all": "1.4.5",
|
||||||
"uglifyjs-webpack-plugin": "^1.2.5",
|
"uglifyjs-webpack-plugin": "1.3.0",
|
||||||
"webpack": "^4.8.0",
|
"webpack": "4.46.0",
|
||||||
"webpack-cli": "^3.1.0",
|
"webpack-cli": "3.3.12",
|
||||||
"webpack-dev-server": "^3.1.4"
|
"webpack-dev-server": "3.11.2"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"scratch-render-fonts": "^1.0.0-prerelease"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"grapheme-breaker": "0.3.2",
|
"grapheme-breaker": "0.3.2",
|
||||||
@@ -53,7 +57,7 @@
|
|||||||
"minilog": "3.1.0",
|
"minilog": "3.1.0",
|
||||||
"raw-loader": "^0.5.1",
|
"raw-loader": "^0.5.1",
|
||||||
"scratch-storage": "^1.0.0",
|
"scratch-storage": "^1.0.0",
|
||||||
"scratch-svg-renderer": "0.2.0-prerelease.20210317184701",
|
"scratch-svg-renderer": "0.2.0-prerelease.20210727023023",
|
||||||
"twgl.js": "4.4.0"
|
"twgl.js": "4.4.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
renovate.json5
Normal file
16
renovate.json5
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||||
|
|
||||||
|
"extends": [
|
||||||
|
"github>LLK/scratch-renovate-config:conservative"
|
||||||
|
],
|
||||||
|
|
||||||
|
"packageRules": [
|
||||||
|
// Don't auto-merge scratch-vm since that will cause a never-ending cycle of dependency updates. Ideally, updating
|
||||||
|
// scratch-vm in scratch-render shouldn't make a scratch-render release. Once that's true we can remove this rule.
|
||||||
|
{
|
||||||
|
"matchPackageNames": ["scratch-vm"],
|
||||||
|
"automerge": false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -653,7 +653,10 @@ class RenderWebGL extends EventEmitter {
|
|||||||
gl.clearColor(...this._backgroundColor4f);
|
gl.clearColor(...this._backgroundColor4f);
|
||||||
gl.clear(gl.COLOR_BUFFER_BIT);
|
gl.clear(gl.COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
this._drawThese(this._drawList, ShaderManager.DRAW_MODE.default, this._projection);
|
this._drawThese(this._drawList, ShaderManager.DRAW_MODE.default, this._projection, {
|
||||||
|
framebufferWidth: gl.canvas.width,
|
||||||
|
framebufferHeight: gl.canvas.height
|
||||||
|
});
|
||||||
if (this._snapshotCallbacks.length > 0) {
|
if (this._snapshotCallbacks.length > 0) {
|
||||||
const snapshot = gl.canvas.toDataURL();
|
const snapshot = gl.canvas.toDataURL();
|
||||||
this._snapshotCallbacks.forEach(cb => cb(snapshot));
|
this._snapshotCallbacks.forEach(cb => cb(snapshot));
|
||||||
@@ -1132,114 +1135,6 @@ class RenderWebGL extends EventEmitter {
|
|||||||
return Number(hit);
|
return Number(hit);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef DrawableExtractionOld
|
|
||||||
* @property {Uint8Array} data Raw pixel data for the drawable
|
|
||||||
* @property {int} width Drawable bounding box width
|
|
||||||
* @property {int} height Drawable bounding box height
|
|
||||||
* @property {Array<number>} scratchOffset [x, y] offset in Scratch coordinates
|
|
||||||
* from the drawable position to the client x, y coordinate
|
|
||||||
* @property {int} x The x coordinate relative to drawable bounding box
|
|
||||||
* @property {int} y The y coordinate relative to drawable bounding box
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return drawable pixel data and picking coordinates relative to the drawable bounds
|
|
||||||
* @param {int} drawableID The ID of the drawable to get pixel data for
|
|
||||||
* @param {int} x The client x coordinate of the picking location.
|
|
||||||
* @param {int} y The client y coordinate of the picking location.
|
|
||||||
* @return {?DrawableExtractionOld} Data about the picked drawable
|
|
||||||
* @deprecated Use {@link extractDrawableScreenSpace} instead.
|
|
||||||
*/
|
|
||||||
extractDrawable (drawableID, x, y) {
|
|
||||||
this._doExitDrawRegion();
|
|
||||||
|
|
||||||
const drawable = this._allDrawables[drawableID];
|
|
||||||
if (!drawable) return null;
|
|
||||||
|
|
||||||
// Convert client coordinates into absolute scratch units
|
|
||||||
const scratchX = this._nativeSize[0] * ((x / this._gl.canvas.clientWidth) - 0.5);
|
|
||||||
const scratchY = this._nativeSize[1] * ((y / this._gl.canvas.clientHeight) - 0.5);
|
|
||||||
|
|
||||||
const gl = this._gl;
|
|
||||||
|
|
||||||
const bounds = drawable.getFastBounds();
|
|
||||||
bounds.snapToInt();
|
|
||||||
|
|
||||||
// Set a reasonable max limit width and height for the bufferInfo bounds
|
|
||||||
const maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
|
|
||||||
const clampedWidth = Math.min(2048, bounds.width, maxTextureSize);
|
|
||||||
const clampedHeight = Math.min(2048, bounds.height, maxTextureSize);
|
|
||||||
|
|
||||||
// Make a new bufferInfo since this._queryBufferInfo is limited to 480x360
|
|
||||||
const attachments = [
|
|
||||||
{format: gl.RGBA},
|
|
||||||
{format: gl.DEPTH_STENCIL}
|
|
||||||
];
|
|
||||||
const bufferInfo = twgl.createFramebufferInfo(gl, attachments, clampedWidth, clampedHeight);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// If the new bufferInfo is invalid, fall back to using the smaller _queryBufferInfo
|
|
||||||
twgl.bindFramebufferInfo(gl, bufferInfo);
|
|
||||||
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE) {
|
|
||||||
twgl.bindFramebufferInfo(gl, this._queryBufferInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Translate to scratch units relative to the drawable
|
|
||||||
const pickX = scratchX - bounds.left;
|
|
||||||
const pickY = scratchY + bounds.top;
|
|
||||||
|
|
||||||
// Limit size of viewport to the bounds around the target Drawable,
|
|
||||||
// and create the projection matrix for the draw.
|
|
||||||
gl.viewport(0, 0, bounds.width, bounds.height);
|
|
||||||
const projection = twgl.m4.ortho(bounds.left, bounds.right, bounds.top, bounds.bottom, -1, 1);
|
|
||||||
|
|
||||||
gl.clearColor(0, 0, 0, 0);
|
|
||||||
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
||||||
try {
|
|
||||||
gl.disable(gl.BLEND);
|
|
||||||
// ImageData objects store alpha un-premultiplied, so draw with the `straightAlpha` draw mode.
|
|
||||||
this._drawThese([drawableID], ShaderManager.DRAW_MODE.straightAlpha, projection,
|
|
||||||
{effectMask: ~ShaderManager.EFFECT_INFO.ghost.mask});
|
|
||||||
} finally {
|
|
||||||
gl.enable(gl.BLEND);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = new Uint8Array(Math.floor(bounds.width * bounds.height * 4));
|
|
||||||
gl.readPixels(0, 0, bounds.width, bounds.height, gl.RGBA, gl.UNSIGNED_BYTE, data);
|
|
||||||
|
|
||||||
if (this._debugCanvas) {
|
|
||||||
this._debugCanvas.width = bounds.width;
|
|
||||||
this._debugCanvas.height = bounds.height;
|
|
||||||
const ctx = this._debugCanvas.getContext('2d');
|
|
||||||
const imageData = ctx.createImageData(bounds.width, bounds.height);
|
|
||||||
imageData.data.set(data);
|
|
||||||
ctx.putImageData(imageData, 0, 0);
|
|
||||||
ctx.beginPath();
|
|
||||||
ctx.arc(pickX, pickY, 3, 0, 2 * Math.PI, false);
|
|
||||||
ctx.fillStyle = 'white';
|
|
||||||
ctx.fill();
|
|
||||||
ctx.lineWidth = 1;
|
|
||||||
ctx.strokeStyle = 'black';
|
|
||||||
ctx.stroke();
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
data: data,
|
|
||||||
width: bounds.width,
|
|
||||||
height: bounds.height,
|
|
||||||
scratchOffset: [
|
|
||||||
-scratchX + drawable._position[0],
|
|
||||||
-scratchY - drawable._position[1]
|
|
||||||
],
|
|
||||||
x: pickX,
|
|
||||||
y: pickY
|
|
||||||
};
|
|
||||||
} finally {
|
|
||||||
gl.deleteFramebuffer(bufferInfo.framebuffer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef DrawableExtraction
|
* @typedef DrawableExtraction
|
||||||
* @property {ImageData} data Raw pixel data for the drawable
|
* @property {ImageData} data Raw pixel data for the drawable
|
||||||
@@ -1317,9 +1212,15 @@ class RenderWebGL extends EventEmitter {
|
|||||||
|
|
||||||
gl.clearColor(0, 0, 0, 0);
|
gl.clearColor(0, 0, 0, 0);
|
||||||
gl.clear(gl.COLOR_BUFFER_BIT);
|
gl.clear(gl.COLOR_BUFFER_BIT);
|
||||||
// Don't apply the ghost effect. TODO: is this an intentional design decision?
|
|
||||||
this._drawThese([drawableID], ShaderManager.DRAW_MODE.straightAlpha, projection,
|
this._drawThese([drawableID], ShaderManager.DRAW_MODE.straightAlpha, projection,
|
||||||
{effectMask: ~ShaderManager.EFFECT_INFO.ghost.mask});
|
{
|
||||||
|
// Don't apply the ghost effect. TODO: is this an intentional design decision?
|
||||||
|
effectMask: ~ShaderManager.EFFECT_INFO.ghost.mask,
|
||||||
|
// We're doing this in screen-space, so the framebuffer dimensions should be those of the canvas in
|
||||||
|
// screen-space. This is used to ensure SVG skins are rendered at the proper resolution.
|
||||||
|
framebufferWidth: canvas.width,
|
||||||
|
framebufferHeight: canvas.height
|
||||||
|
});
|
||||||
|
|
||||||
const data = new Uint8Array(Math.floor(clampedWidth * clampedHeight * 4));
|
const data = new Uint8Array(Math.floor(clampedWidth * clampedHeight * 4));
|
||||||
gl.readPixels(0, 0, clampedWidth, clampedHeight, gl.RGBA, gl.UNSIGNED_BYTE, data);
|
gl.readPixels(0, 0, clampedWidth, clampedHeight, gl.RGBA, gl.UNSIGNED_BYTE, data);
|
||||||
@@ -1821,18 +1722,6 @@ class RenderWebGL extends EventEmitter {
|
|||||||
this._regionId = null;
|
this._regionId = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the screen-space scale of a drawable, as percentages of the drawable's "normal" size.
|
|
||||||
* @param {Drawable} drawable The drawable whose screen-space scale we're fetching.
|
|
||||||
* @returns {Array<number>} The screen-space X and Y dimensions of the drawable's scale, as percentages.
|
|
||||||
*/
|
|
||||||
_getDrawableScreenSpaceScale (drawable) {
|
|
||||||
return [
|
|
||||||
drawable.scale[0] * this._gl.canvas.width / this._nativeSize[0],
|
|
||||||
drawable.scale[1] * this._gl.canvas.height / this._nativeSize[1]
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Draw a set of Drawables, by drawable ID
|
* Draw a set of Drawables, by drawable ID
|
||||||
* @param {Array<int>} drawables The Drawable IDs to draw, possibly this._drawList.
|
* @param {Array<int>} drawables The Drawable IDs to draw, possibly this._drawList.
|
||||||
@@ -1843,6 +1732,8 @@ class RenderWebGL extends EventEmitter {
|
|||||||
* @param {object.<string,*>} opts.extraUniforms Extra uniforms for the shaders.
|
* @param {object.<string,*>} opts.extraUniforms Extra uniforms for the shaders.
|
||||||
* @param {int} opts.effectMask Bitmask for effects to allow
|
* @param {int} opts.effectMask Bitmask for effects to allow
|
||||||
* @param {boolean} opts.ignoreVisibility Draw all, despite visibility (e.g. stamping, touching color)
|
* @param {boolean} opts.ignoreVisibility Draw all, despite visibility (e.g. stamping, touching color)
|
||||||
|
* @param {int} opts.framebufferWidth The width of the framebuffer being drawn onto. Defaults to "native" width
|
||||||
|
* @param {int} opts.framebufferHeight The height of the framebuffer being drawn onto. Defaults to "native" height
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_drawThese (drawables, drawMode, projection, opts = {}) {
|
_drawThese (drawables, drawMode, projection, opts = {}) {
|
||||||
@@ -1850,6 +1741,11 @@ class RenderWebGL extends EventEmitter {
|
|||||||
const gl = this._gl;
|
const gl = this._gl;
|
||||||
let currentShader = null;
|
let currentShader = null;
|
||||||
|
|
||||||
|
const framebufferSpaceScaleDiffers = (
|
||||||
|
'framebufferWidth' in opts && 'framebufferHeight' in opts &&
|
||||||
|
opts.framebufferWidth !== this._nativeSize[0] && opts.framebufferHeight !== this._nativeSize[1]
|
||||||
|
);
|
||||||
|
|
||||||
const numDrawables = drawables.length;
|
const numDrawables = drawables.length;
|
||||||
for (let drawableIndex = 0; drawableIndex < numDrawables; ++drawableIndex) {
|
for (let drawableIndex = 0; drawableIndex < numDrawables; ++drawableIndex) {
|
||||||
const drawableID = drawables[drawableIndex];
|
const drawableID = drawables[drawableIndex];
|
||||||
@@ -1864,8 +1760,13 @@ class RenderWebGL extends EventEmitter {
|
|||||||
// the ignoreVisibility flag is used (e.g. for stamping or touchingColor).
|
// the ignoreVisibility flag is used (e.g. for stamping or touchingColor).
|
||||||
if (!drawable.getVisible() && !opts.ignoreVisibility) continue;
|
if (!drawable.getVisible() && !opts.ignoreVisibility) continue;
|
||||||
|
|
||||||
// Combine drawable scale with the native vs. backing pixel ratio
|
// drawableScale is the "framebuffer-pixel-space" scale of the drawable, as percentages of the drawable's
|
||||||
const drawableScale = this._getDrawableScreenSpaceScale(drawable);
|
// "native size" (so 100 = same as skin's "native size", 200 = twice "native size").
|
||||||
|
// If the framebuffer dimensions are the same as the stage's "native" size, there's no need to calculate it.
|
||||||
|
const drawableScale = framebufferSpaceScaleDiffers ? [
|
||||||
|
drawable.scale[0] * opts.framebufferWidth / this._nativeSize[0],
|
||||||
|
drawable.scale[1] * opts.framebufferHeight / this._nativeSize[1]
|
||||||
|
] : drawable.scale;
|
||||||
|
|
||||||
// If the skin or texture isn't ready yet, skip it.
|
// If the skin or texture isn't ready yet, skip it.
|
||||||
if (!drawable.skin || !drawable.skin.getTexture(drawableScale)) continue;
|
if (!drawable.skin || !drawable.skin.getTexture(drawableScale)) continue;
|
||||||
@@ -2097,7 +1998,7 @@ class RenderWebGL extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// :3
|
// :3
|
||||||
RenderWebGL.prototype.canHazPixels = RenderWebGL.prototype.extractDrawable;
|
RenderWebGL.prototype.canHazPixels = RenderWebGL.prototype.extractDrawableScreenSpace;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Values for setUseGPU()
|
* Values for setUseGPU()
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const twgl = require('twgl.js');
|
const twgl = require('twgl.js');
|
||||||
|
|
||||||
const Skin = require('./Skin');
|
const Skin = require('./Skin');
|
||||||
const SvgRenderer = require('scratch-svg-renderer').SVGRenderer;
|
const {loadSvgString, serializeSvgToString} = require('scratch-svg-renderer');
|
||||||
const ShaderManager = require('./ShaderManager');
|
const ShaderManager = require('./ShaderManager');
|
||||||
|
|
||||||
const MAX_TEXTURE_DIMENSION = 2048;
|
const MAX_TEXTURE_DIMENSION = 2048;
|
||||||
@@ -28,8 +28,20 @@ class SVGSkin extends Skin {
|
|||||||
/** @type {RenderWebGL} */
|
/** @type {RenderWebGL} */
|
||||||
this._renderer = renderer;
|
this._renderer = renderer;
|
||||||
|
|
||||||
/** @type {SvgRenderer} */
|
/** @type {HTMLImageElement} */
|
||||||
this._svgRenderer = new SvgRenderer();
|
this._svgImage = document.createElement('img');
|
||||||
|
|
||||||
|
/** @type {boolean} */
|
||||||
|
this._svgImageLoaded = false;
|
||||||
|
|
||||||
|
/** @type {Array<number>} */
|
||||||
|
this._size = [0, 0];
|
||||||
|
|
||||||
|
/** @type {HTMLCanvasElement} */
|
||||||
|
this._canvas = document.createElement('canvas');
|
||||||
|
|
||||||
|
/** @type {CanvasRenderingContext2D} */
|
||||||
|
this._context = this._canvas.getContext('2d');
|
||||||
|
|
||||||
/** @type {Array<WebGLTexture>} */
|
/** @type {Array<WebGLTexture>} */
|
||||||
this._scaledMIPs = [];
|
this._scaledMIPs = [];
|
||||||
@@ -56,7 +68,7 @@ class SVGSkin extends Skin {
|
|||||||
* @return {Array<number>} the natural size, in Scratch units, of this skin.
|
* @return {Array<number>} the natural size, in Scratch units, of this skin.
|
||||||
*/
|
*/
|
||||||
get size () {
|
get size () {
|
||||||
return this._svgRenderer.size;
|
return [this._size[0], this._size[1]];
|
||||||
}
|
}
|
||||||
|
|
||||||
useNearest (scale, drawable) {
|
useNearest (scale, drawable) {
|
||||||
@@ -94,18 +106,27 @@ class SVGSkin extends Skin {
|
|||||||
* @return {SVGMIP} An object that handles creating and updating SVG textures.
|
* @return {SVGMIP} An object that handles creating and updating SVG textures.
|
||||||
*/
|
*/
|
||||||
createMIP (scale) {
|
createMIP (scale) {
|
||||||
this._svgRenderer.draw(scale);
|
const [width, height] = this._size;
|
||||||
|
this._canvas.width = width * scale;
|
||||||
|
this._canvas.height = height * scale;
|
||||||
|
if (
|
||||||
|
this._canvas.width <= 0 ||
|
||||||
|
this._canvas.height <= 0 ||
|
||||||
|
// Even if the canvas at the current scale has a nonzero size, the image's dimensions are floored
|
||||||
|
// pre-scaling; e.g. if an image has a width of 0.4 and is being rendered at 3x scale, the canvas will have
|
||||||
|
// a width of 1, but the image's width will be rounded down to 0 on some browsers (Firefox) prior to being
|
||||||
|
// drawn at that scale, resulting in an IndexSizeError if we attempt to draw it.
|
||||||
|
this._svgImage.naturalWidth <= 0 ||
|
||||||
|
this._svgImage.naturalHeight <= 0
|
||||||
|
) return super.getTexture();
|
||||||
|
this._context.clearRect(0, 0, this._canvas.width, this._canvas.height);
|
||||||
|
this._context.setTransform(scale, 0, 0, scale, 0, 0);
|
||||||
|
this._context.drawImage(this._svgImage, 0, 0);
|
||||||
|
|
||||||
// Pull out the ImageData from the canvas. ImageData speeds up
|
// Pull out the ImageData from the canvas. ImageData speeds up
|
||||||
// updating Silhouette and is better handled by more browsers in
|
// updating Silhouette and is better handled by more browsers in
|
||||||
// regards to memory.
|
// regards to memory.
|
||||||
const canvas = this._svgRenderer.canvas;
|
const textureData = this._context.getImageData(0, 0, this._canvas.width, this._canvas.height);
|
||||||
// If one of the canvas dimensions is 0, set this MIP to an empty image texture.
|
|
||||||
// This avoids an IndexSizeError from attempting to getImageData when one of the dimensions is 0.
|
|
||||||
if (canvas.width === 0 || canvas.height === 0) return super.getTexture();
|
|
||||||
|
|
||||||
const context = canvas.getContext('2d');
|
|
||||||
const textureData = context.getImageData(0, 0, canvas.width, canvas.height);
|
|
||||||
|
|
||||||
const textureOptions = {
|
const textureOptions = {
|
||||||
auto: false,
|
auto: false,
|
||||||
@@ -147,7 +168,7 @@ class SVGSkin extends Skin {
|
|||||||
// Can't use bitwise stuff here because we need to handle negative exponents
|
// Can't use bitwise stuff here because we need to handle negative exponents
|
||||||
const mipScale = Math.pow(2, mipLevel - INDEX_OFFSET);
|
const mipScale = Math.pow(2, mipLevel - INDEX_OFFSET);
|
||||||
|
|
||||||
if (this._svgRenderer.loaded && !this._scaledMIPs[mipLevel]) {
|
if (this._svgImageLoaded && !this._scaledMIPs[mipLevel]) {
|
||||||
this._scaledMIPs[mipLevel] = this.createMIP(mipScale);
|
this._scaledMIPs[mipLevel] = this.createMIP(mipScale);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,14 +192,22 @@ class SVGSkin extends Skin {
|
|||||||
* @fires Skin.event:WasAltered
|
* @fires Skin.event:WasAltered
|
||||||
*/
|
*/
|
||||||
setSVG (svgData, rotationCenter) {
|
setSVG (svgData, rotationCenter) {
|
||||||
this._svgRenderer.loadSVG(svgData, false, () => {
|
const svgTag = loadSvgString(svgData);
|
||||||
const svgSize = this._svgRenderer.size;
|
const svgText = serializeSvgToString(svgTag, true /* shouldInjectFonts */);
|
||||||
if (svgSize[0] === 0 || svgSize[1] === 0) {
|
this._svgImageLoaded = false;
|
||||||
|
|
||||||
|
// If there is another load already in progress, replace the old onload to effectively cancel the old load
|
||||||
|
this._svgImage.onload = () => {
|
||||||
|
const {x, y, width, height} = svgTag.viewBox.baseVal;
|
||||||
|
this._size[0] = width;
|
||||||
|
this._size[1] = height;
|
||||||
|
|
||||||
|
if (width === 0 || height === 0) {
|
||||||
super.setEmptyImageData();
|
super.setEmptyImageData();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxDimension = Math.ceil(Math.max(this.size[0], this.size[1]));
|
const maxDimension = Math.ceil(Math.max(width, height));
|
||||||
let testScale = 2;
|
let testScale = 2;
|
||||||
for (testScale; maxDimension * testScale <= MAX_TEXTURE_DIMENSION; testScale *= 2) {
|
for (testScale; maxDimension * testScale <= MAX_TEXTURE_DIMENSION; testScale *= 2) {
|
||||||
this._maxTextureScale = testScale;
|
this._maxTextureScale = testScale;
|
||||||
@@ -187,12 +216,17 @@ class SVGSkin extends Skin {
|
|||||||
this.resetMIPs();
|
this.resetMIPs();
|
||||||
|
|
||||||
if (typeof rotationCenter === 'undefined') rotationCenter = this.calculateRotationCenter();
|
if (typeof rotationCenter === 'undefined') rotationCenter = this.calculateRotationCenter();
|
||||||
const viewOffset = this._svgRenderer.viewOffset;
|
// Compensate for viewbox offset.
|
||||||
this._rotationCenter[0] = rotationCenter[0] - viewOffset[0];
|
// See https://github.com/LLK/scratch-render/pull/90.
|
||||||
this._rotationCenter[1] = rotationCenter[1] - viewOffset[1];
|
this._rotationCenter[0] = rotationCenter[0] - x;
|
||||||
|
this._rotationCenter[1] = rotationCenter[1] - y;
|
||||||
|
|
||||||
|
this._svgImageLoaded = true;
|
||||||
|
|
||||||
this.emit(Skin.Events.WasAltered);
|
this.emit(Skin.Events.WasAltered);
|
||||||
});
|
};
|
||||||
|
|
||||||
|
this._svgImage.src = `data:image/svg+xml;utf8,${encodeURIComponent(svgText)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -186,7 +186,7 @@ canvas.addEventListener('click', event => {
|
|||||||
const pickID = renderer.pick(mousePos.x, mousePos.y);
|
const pickID = renderer.pick(mousePos.x, mousePos.y);
|
||||||
console.log(`You clicked on ${(pickID < 0 ? 'nothing' : `ID# ${pickID}`)}`);
|
console.log(`You clicked on ${(pickID < 0 ? 'nothing' : `ID# ${pickID}`)}`);
|
||||||
if (pickID >= 0) {
|
if (pickID >= 0) {
|
||||||
console.dir(renderer.extractDrawable(pickID, mousePos.x, mousePos.y));
|
console.dir(renderer.extractDrawableScreenSpace(pickID, mousePos.x, mousePos.y));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ window.waitForSVGSkinLoad = renderer => new Promise(resolve => {
|
|||||||
for (const skin of renderer._allSkins) {
|
for (const skin of renderer._allSkins) {
|
||||||
if (skin.constructor.name !== 'SVGSkin') continue;
|
if (skin.constructor.name !== 'SVGSkin') continue;
|
||||||
numSVGSkins++;
|
numSVGSkins++;
|
||||||
if (skin._svgRenderer.loaded) numLoadedSVGSkins++;
|
if (skin._svgImage.complete) numLoadedSVGSkins++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (numSVGSkins === numLoadedSVGSkins) {
|
if (numSVGSkins === numLoadedSVGSkins) {
|
||||||
@@ -47,7 +47,7 @@ window.initVM = render => {
|
|||||||
|
|
||||||
vm.attachStorage(storage);
|
vm.attachStorage(storage);
|
||||||
vm.attachRenderer(render);
|
vm.attachRenderer(render);
|
||||||
vm.attachV2SVGAdapter(new ScratchSVGRenderer.SVGRenderer());
|
vm.attachV2SVGAdapter(ScratchSVGRenderer.V2SVGAdapter);
|
||||||
vm.attachV2BitmapAdapter(new ScratchSVGRenderer.BitmapAdapter());
|
vm.attachV2BitmapAdapter(new ScratchSVGRenderer.BitmapAdapter());
|
||||||
|
|
||||||
return vm;
|
return vm;
|
||||||
|
|||||||
Reference in New Issue
Block a user