修复缩放
This commit is contained in:
1 parent
e2df519d2e
commit
e60506ef96
3 files changed
+20
-8
No files matched your search
@@ -491,11 +491,11 @@
|
|||||||
// baseFitScale 为自动适配用的基准缩放;panX/panY/zoom 为用户平移/缩放叠加。
|
// baseFitScale 为自动适配用的基准缩放;panX/panY/zoom 为用户平移/缩放叠加。
|
||||||
// fitScale = baseFitScale * zoom;fitCx/fitCy 是骨骼包围盒中心(世界坐标)。
|
// fitScale = baseFitScale * zoom;fitCx/fitCy 是骨骼包围盒中心(世界坐标)。
|
||||||
let fitScale = 1, fitCx = 0, fitCy = 0;
|
let fitScale = 1, fitCx = 0, fitCy = 0;
|
||||||
function captureStableFitBounds() {
|
function captureStableFitBounds(bounds = getBounds(skeleton)) {
|
||||||
const bounds = getBounds(skeleton);
|
|
||||||
stableFitBounds = bounds && bounds.width > 0 && bounds.height > 0
|
stableFitBounds = bounds && bounds.width > 0 && bounds.height > 0
|
||||||
? { x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height }
|
? { x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height }
|
||||||
: null;
|
: null;
|
||||||
|
return stableFitBounds !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function fitToCanvas() {
|
function fitToCanvas() {
|
||||||
@@ -504,7 +504,7 @@
|
|||||||
const bounds = stableFitBounds;
|
const bounds = stableFitBounds;
|
||||||
const cw = canvas.clientWidth, ch = canvas.clientHeight;
|
const cw = canvas.clientWidth, ch = canvas.clientHeight;
|
||||||
if (!bounds || bounds.width <= 0 || bounds.height <= 0) {
|
if (!bounds || bounds.width <= 0 || bounds.height <= 0) {
|
||||||
baseFitScale = 1; baseFitCx = 0; baseFitCy = 0; fitScale = 1; fitCx = 0; fitCy = 0; return;
|
baseFitScale = 1; baseFitCx = 0; baseFitCy = 0; fitScale = zoom; fitCx = 0; fitCy = 0; return;
|
||||||
}
|
}
|
||||||
const pad = 0.1;
|
const pad = 0.1;
|
||||||
const sx = (cw * (1 - pad * 2)) / bounds.width;
|
const sx = (cw * (1 - pad * 2)) / bounds.width;
|
||||||
@@ -1440,7 +1440,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setAnimationByName(name, index) {
|
async function setAnimationByName(name, index) {
|
||||||
if (!state) return;
|
if (!state) return;
|
||||||
const anim = findAnimByName(name);
|
const anim = findAnimByName(name);
|
||||||
if (!anim) return;
|
if (!anim) return;
|
||||||
@@ -1452,6 +1452,11 @@
|
|||||||
state.update(0);
|
state.update(0);
|
||||||
state.apply(skeleton);
|
state.apply(skeleton);
|
||||||
updateWorld(skeleton);
|
updateWorld(skeleton);
|
||||||
|
if (!stableFitBounds) {
|
||||||
|
const geometryBounds = getBounds(skeleton);
|
||||||
|
const bounds = await measureVisibleBounds(skeleton, geometryBounds, 1, () => {}) || geometryBounds;
|
||||||
|
if (currentAnim === resolvedIndex && captureStableFitBounds(bounds)) fitToCanvas();
|
||||||
|
}
|
||||||
draw();
|
draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,9 +31,11 @@ for (const root of roots) {
|
|||||||
const fit = functionBody(script, 'fitToCanvas', 'draw');
|
const fit = functionBody(script, 'fitToCanvas', 'draw');
|
||||||
assert.match(fit, /const bounds = stableFitBounds;/);
|
assert.match(fit, /const bounds = stableFitBounds;/);
|
||||||
assert.doesNotMatch(fit, /getBounds\(/, 'wheel/resize fitting must not sample an animated frame');
|
assert.doesNotMatch(fit, /getBounds\(/, 'wheel/resize fitting must not sample an animated frame');
|
||||||
|
assert.match(fit, /fitScale = zoom;/, 'wheel zoom must still work before a drawable stable bound exists');
|
||||||
|
|
||||||
const capture = functionBody(script, 'captureStableFitBounds', 'fitToCanvas');
|
const capture = functionBody(script, 'captureStableFitBounds', 'fitToCanvas');
|
||||||
assert.match(capture, /getBounds\(skeleton\)/);
|
assert.match(capture, /getBounds\(skeleton\)/);
|
||||||
|
assert.match(script, /async function setAnimationByName[\s\S]*if \(!stableFitBounds\)[\s\S]*measureVisibleBounds\(skeleton, geometryBounds, 1[\s\S]*captureStableFitBounds\(bounds\)/);
|
||||||
assert.match(css, /#status \{\s*position: absolute; left: 50%;[^}]*transform: translateX\(-50%\)/, 'status notice must be horizontally centered');
|
assert.match(css, /#status \{\s*position: absolute; left: 50%;[^}]*transform: translateX\(-50%\)/, 'status notice must be horizontally centered');
|
||||||
|
|
||||||
const manifest = JSON.parse(fs.readFileSync(path.join(root, 'manifest.json'), 'utf8'));
|
const manifest = JSON.parse(fs.readFileSync(path.join(root, 'manifest.json'), 'utf8'));
|
||||||
|
|||||||
+9
-4
@@ -491,11 +491,11 @@
|
|||||||
// baseFitScale 为自动适配用的基准缩放;panX/panY/zoom 为用户平移/缩放叠加。
|
// baseFitScale 为自动适配用的基准缩放;panX/panY/zoom 为用户平移/缩放叠加。
|
||||||
// fitScale = baseFitScale * zoom;fitCx/fitCy 是骨骼包围盒中心(世界坐标)。
|
// fitScale = baseFitScale * zoom;fitCx/fitCy 是骨骼包围盒中心(世界坐标)。
|
||||||
let fitScale = 1, fitCx = 0, fitCy = 0;
|
let fitScale = 1, fitCx = 0, fitCy = 0;
|
||||||
function captureStableFitBounds() {
|
function captureStableFitBounds(bounds = getBounds(skeleton)) {
|
||||||
const bounds = getBounds(skeleton);
|
|
||||||
stableFitBounds = bounds && bounds.width > 0 && bounds.height > 0
|
stableFitBounds = bounds && bounds.width > 0 && bounds.height > 0
|
||||||
? { x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height }
|
? { x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height }
|
||||||
: null;
|
: null;
|
||||||
|
return stableFitBounds !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function fitToCanvas() {
|
function fitToCanvas() {
|
||||||
@@ -504,7 +504,7 @@
|
|||||||
const bounds = stableFitBounds;
|
const bounds = stableFitBounds;
|
||||||
const cw = canvas.clientWidth, ch = canvas.clientHeight;
|
const cw = canvas.clientWidth, ch = canvas.clientHeight;
|
||||||
if (!bounds || bounds.width <= 0 || bounds.height <= 0) {
|
if (!bounds || bounds.width <= 0 || bounds.height <= 0) {
|
||||||
baseFitScale = 1; baseFitCx = 0; baseFitCy = 0; fitScale = 1; fitCx = 0; fitCy = 0; return;
|
baseFitScale = 1; baseFitCx = 0; baseFitCy = 0; fitScale = zoom; fitCx = 0; fitCy = 0; return;
|
||||||
}
|
}
|
||||||
const pad = 0.1;
|
const pad = 0.1;
|
||||||
const sx = (cw * (1 - pad * 2)) / bounds.width;
|
const sx = (cw * (1 - pad * 2)) / bounds.width;
|
||||||
@@ -1440,7 +1440,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setAnimationByName(name, index) {
|
async function setAnimationByName(name, index) {
|
||||||
if (!state) return;
|
if (!state) return;
|
||||||
const anim = findAnimByName(name);
|
const anim = findAnimByName(name);
|
||||||
if (!anim) return;
|
if (!anim) return;
|
||||||
@@ -1452,6 +1452,11 @@
|
|||||||
state.update(0);
|
state.update(0);
|
||||||
state.apply(skeleton);
|
state.apply(skeleton);
|
||||||
updateWorld(skeleton);
|
updateWorld(skeleton);
|
||||||
|
if (!stableFitBounds) {
|
||||||
|
const geometryBounds = getBounds(skeleton);
|
||||||
|
const bounds = await measureVisibleBounds(skeleton, geometryBounds, 1, () => {}) || geometryBounds;
|
||||||
|
if (currentAnim === resolvedIndex && captureStableFitBounds(bounds)) fitToCanvas();
|
||||||
|
}
|
||||||
draw();
|
draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user