Remove last get/setTransform (#964)
My original hack to put the scale when we create the canvas element doesn't make much sense. It should be done when we are rendering the scene. I moved it there in this PR. The rest was all about forwarding the scale to where it's needed.
This commit is contained in:
parent
79ea76b48b
commit
2937efacde
@ -682,6 +682,7 @@ export class App extends React.Component<any, AppState> {
|
|||||||
{ clientX: cursorX, clientY: cursorY },
|
{ clientX: cursorX, clientY: cursorY },
|
||||||
this.state,
|
this.state,
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
|
|
||||||
const element = newTextElement(
|
const element = newTextElement(
|
||||||
@ -808,10 +809,6 @@ export class App extends React.Component<any, AppState> {
|
|||||||
this.canvas.addEventListener("wheel", this.handleWheel, {
|
this.canvas.addEventListener("wheel", this.handleWheel, {
|
||||||
passive: false,
|
passive: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.canvas
|
|
||||||
.getContext("2d")
|
|
||||||
?.setTransform(canvasScale, 0, 0, canvasScale, 0, 0);
|
|
||||||
} else {
|
} else {
|
||||||
this.canvas?.removeEventListener("wheel", this.handleWheel);
|
this.canvas?.removeEventListener("wheel", this.handleWheel);
|
||||||
}
|
}
|
||||||
@ -823,6 +820,7 @@ export class App extends React.Component<any, AppState> {
|
|||||||
event,
|
event,
|
||||||
this.state,
|
this.state,
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
|
|
||||||
const element = getElementAtPosition(
|
const element = getElementAtPosition(
|
||||||
@ -923,6 +921,7 @@ export class App extends React.Component<any, AppState> {
|
|||||||
event,
|
event,
|
||||||
this.state,
|
this.state,
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
|
|
||||||
const elementAtPosition = getElementAtPosition(
|
const elementAtPosition = getElementAtPosition(
|
||||||
@ -974,6 +973,7 @@ export class App extends React.Component<any, AppState> {
|
|||||||
{ sceneX: centerElementX, sceneY: centerElementY },
|
{ sceneX: centerElementX, sceneY: centerElementY },
|
||||||
this.state,
|
this.state,
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
|
|
||||||
textX = centerElementXInViewport;
|
textX = centerElementXInViewport;
|
||||||
@ -1048,6 +1048,7 @@ export class App extends React.Component<any, AppState> {
|
|||||||
event,
|
event,
|
||||||
this.state,
|
this.state,
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
this.savePointer(pointerCoords);
|
this.savePointer(pointerCoords);
|
||||||
if (gesture.pointers.has(event.pointerId)) {
|
if (gesture.pointers.has(event.pointerId)) {
|
||||||
@ -1096,6 +1097,7 @@ export class App extends React.Component<any, AppState> {
|
|||||||
event,
|
event,
|
||||||
this.state,
|
this.state,
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
if (this.state.multiElement) {
|
if (this.state.multiElement) {
|
||||||
const { multiElement } = this.state;
|
const { multiElement } = this.state;
|
||||||
@ -1247,6 +1249,7 @@ export class App extends React.Component<any, AppState> {
|
|||||||
event,
|
event,
|
||||||
this.state,
|
this.state,
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
let lastX = x;
|
let lastX = x;
|
||||||
let lastY = y;
|
let lastY = y;
|
||||||
@ -1657,6 +1660,7 @@ export class App extends React.Component<any, AppState> {
|
|||||||
event,
|
event,
|
||||||
this.state,
|
this.state,
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
if (distance2d(x, y, originX, originY) < DRAGGING_THRESHOLD) {
|
if (distance2d(x, y, originX, originY) < DRAGGING_THRESHOLD) {
|
||||||
return;
|
return;
|
||||||
@ -1675,6 +1679,7 @@ export class App extends React.Component<any, AppState> {
|
|||||||
event,
|
event,
|
||||||
this.state,
|
this.state,
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
const deltaX = x - lastX;
|
const deltaX = x - lastX;
|
||||||
const deltaY = y - lastY;
|
const deltaY = y - lastY;
|
||||||
@ -1891,6 +1896,7 @@ export class App extends React.Component<any, AppState> {
|
|||||||
event,
|
event,
|
||||||
this.state,
|
this.state,
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
|
|
||||||
selectedElements.forEach(element => {
|
selectedElements.forEach(element => {
|
||||||
@ -1916,6 +1922,7 @@ export class App extends React.Component<any, AppState> {
|
|||||||
event,
|
event,
|
||||||
this.state,
|
this.state,
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
|
|
||||||
let width = distance(originX, x);
|
let width = distance(originX, x);
|
||||||
@ -2019,6 +2026,7 @@ export class App extends React.Component<any, AppState> {
|
|||||||
event,
|
event,
|
||||||
this.state,
|
this.state,
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
mutateElement(draggingElement, {
|
mutateElement(draggingElement, {
|
||||||
points: [
|
points: [
|
||||||
@ -2184,6 +2192,7 @@ export class App extends React.Component<any, AppState> {
|
|||||||
{ clientX: cursorX, clientY: cursorY },
|
{ clientX: cursorX, clientY: cursorY },
|
||||||
this.state,
|
this.state,
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
|
|
||||||
const dx = x - elementsCenterX;
|
const dx = x - elementsCenterX;
|
||||||
@ -2270,6 +2279,7 @@ export class App extends React.Component<any, AppState> {
|
|||||||
},
|
},
|
||||||
this.state,
|
this.state,
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
const { atLeastOneVisibleElement, scrollBars } = renderScene(
|
const { atLeastOneVisibleElement, scrollBars } = renderScene(
|
||||||
|
@ -64,6 +64,10 @@ function generateElementCanvas(
|
|||||||
const rc = rough.canvas(canvas);
|
const rc = rough.canvas(canvas);
|
||||||
drawElementOnCanvas(element, rc, context);
|
drawElementOnCanvas(element, rc, context);
|
||||||
context.translate(-CANVAS_PADDING, -CANVAS_PADDING);
|
context.translate(-CANVAS_PADDING, -CANVAS_PADDING);
|
||||||
|
context.scale(
|
||||||
|
1 / (window.devicePixelRatio * zoom),
|
||||||
|
1 / (window.devicePixelRatio * zoom),
|
||||||
|
);
|
||||||
return { element, canvas, canvasZoom: zoom, canvasOffsetX, canvasOffsetY };
|
return { element, canvas, canvasZoom: zoom, canvasOffsetX, canvasOffsetY };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,6 +55,7 @@ export function renderScene(
|
|||||||
const elements = allElements.filter(element => !element.isDeleted);
|
const elements = allElements.filter(element => !element.isDeleted);
|
||||||
|
|
||||||
const context = canvas.getContext("2d")!;
|
const context = canvas.getContext("2d")!;
|
||||||
|
context.scale(scale, scale);
|
||||||
|
|
||||||
// When doing calculations based on canvas width we should used normalized one
|
// When doing calculations based on canvas width we should used normalized one
|
||||||
const normalizedCanvasWidth = canvas.width / scale;
|
const normalizedCanvasWidth = canvas.width / scale;
|
||||||
@ -205,8 +206,9 @@ export function renderScene(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Paint scrollbars
|
// Paint scrollbars
|
||||||
|
let scrollBars;
|
||||||
if (renderScrollbars) {
|
if (renderScrollbars) {
|
||||||
const scrollBars = getScrollBars(
|
scrollBars = getScrollBars(
|
||||||
elements,
|
elements,
|
||||||
normalizedCanvasWidth,
|
normalizedCanvasWidth,
|
||||||
normalizedCanvasHeight,
|
normalizedCanvasHeight,
|
||||||
@ -231,10 +233,11 @@ export function renderScene(
|
|||||||
});
|
});
|
||||||
context.fillStyle = fillStyle;
|
context.fillStyle = fillStyle;
|
||||||
context.strokeStyle = strokeStyle;
|
context.strokeStyle = strokeStyle;
|
||||||
return { atLeastOneVisibleElement: visibleElements.length > 0, scrollBars };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { atLeastOneVisibleElement: visibleElements.length > 0 };
|
context.scale(1 / scale, 1 / scale);
|
||||||
|
|
||||||
|
return { atLeastOneVisibleElement: visibleElements.length > 0, scrollBars };
|
||||||
}
|
}
|
||||||
|
|
||||||
function isVisibleElement(
|
function isVisibleElement(
|
||||||
|
@ -36,7 +36,6 @@ export function exportToCanvas(
|
|||||||
const height = distance(minY, maxY) + exportPadding * 2;
|
const height = distance(minY, maxY) + exportPadding * 2;
|
||||||
|
|
||||||
const tempCanvas: any = createCanvas(width, height);
|
const tempCanvas: any = createCanvas(width, height);
|
||||||
tempCanvas.getContext("2d")?.scale(scale, scale);
|
|
||||||
|
|
||||||
renderScene(
|
renderScene(
|
||||||
elements,
|
elements,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
export function getZoomOrigin(canvas: HTMLCanvasElement | null) {
|
export function getZoomOrigin(canvas: HTMLCanvasElement | null, scale: number) {
|
||||||
if (canvas === null) {
|
if (canvas === null) {
|
||||||
return { x: 0, y: 0 };
|
return { x: 0, y: 0 };
|
||||||
}
|
}
|
||||||
@ -7,8 +7,8 @@ export function getZoomOrigin(canvas: HTMLCanvasElement | null) {
|
|||||||
return { x: 0, y: 0 };
|
return { x: 0, y: 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
const normalizedCanvasWidth = canvas.width / context.getTransform().a;
|
const normalizedCanvasWidth = canvas.width / scale;
|
||||||
const normalizedCanvasHeight = canvas.height / context.getTransform().d;
|
const normalizedCanvasHeight = canvas.height / scale;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
x: normalizedCanvasWidth / 2,
|
x: normalizedCanvasWidth / 2,
|
||||||
|
@ -157,8 +157,9 @@ export function viewportCoordsToSceneCoords(
|
|||||||
zoom: number;
|
zoom: number;
|
||||||
},
|
},
|
||||||
canvas: HTMLCanvasElement | null,
|
canvas: HTMLCanvasElement | null,
|
||||||
|
scale: number,
|
||||||
) {
|
) {
|
||||||
const zoomOrigin = getZoomOrigin(canvas);
|
const zoomOrigin = getZoomOrigin(canvas, scale);
|
||||||
const clientXWithZoom = zoomOrigin.x + (clientX - zoomOrigin.x) / zoom;
|
const clientXWithZoom = zoomOrigin.x + (clientX - zoomOrigin.x) / zoom;
|
||||||
const clientYWithZoom = zoomOrigin.y + (clientY - zoomOrigin.y) / zoom;
|
const clientYWithZoom = zoomOrigin.y + (clientY - zoomOrigin.y) / zoom;
|
||||||
|
|
||||||
@ -180,8 +181,9 @@ export function sceneCoordsToViewportCoords(
|
|||||||
zoom: number;
|
zoom: number;
|
||||||
},
|
},
|
||||||
canvas: HTMLCanvasElement | null,
|
canvas: HTMLCanvasElement | null,
|
||||||
|
scale: number,
|
||||||
) {
|
) {
|
||||||
const zoomOrigin = getZoomOrigin(canvas);
|
const zoomOrigin = getZoomOrigin(canvas, scale);
|
||||||
const sceneXWithZoomAndScroll =
|
const sceneXWithZoomAndScroll =
|
||||||
zoomOrigin.x - (zoomOrigin.x - sceneX - scrollX) * zoom;
|
zoomOrigin.x - (zoomOrigin.x - sceneX - scrollX) * zoom;
|
||||||
const sceneYWithZoomAndScroll =
|
const sceneYWithZoomAndScroll =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user