Use device pixel ratio (#139)
* Use device pixel ratio * Update index.tsx
This commit is contained in:
parent
4ad49ff970
commit
4ca8f65887
@ -361,9 +361,9 @@ function renderScene(
|
|||||||
const fillStyle = context.fillStyle;
|
const fillStyle = context.fillStyle;
|
||||||
if (typeof sceneState.viewBackgroundColor === "string") {
|
if (typeof sceneState.viewBackgroundColor === "string") {
|
||||||
context.fillStyle = sceneState.viewBackgroundColor;
|
context.fillStyle = sceneState.viewBackgroundColor;
|
||||||
context.fillRect(-0.5, -0.5, canvas.width, canvas.height);
|
context.fillRect(0, 0, canvas.width, canvas.height);
|
||||||
} else {
|
} else {
|
||||||
context.clearRect(-0.5, -0.5, canvas.width, canvas.height);
|
context.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
}
|
}
|
||||||
context.fillStyle = fillStyle;
|
context.fillStyle = fillStyle;
|
||||||
|
|
||||||
@ -409,8 +409,8 @@ function renderScene(
|
|||||||
|
|
||||||
if (renderScrollbars) {
|
if (renderScrollbars) {
|
||||||
const scrollBars = getScrollbars(
|
const scrollBars = getScrollbars(
|
||||||
context.canvas.width,
|
context.canvas.width / window.devicePixelRatio,
|
||||||
context.canvas.height,
|
context.canvas.height / window.devicePixelRatio,
|
||||||
sceneState.scrollX,
|
sceneState.scrollX,
|
||||||
sceneState.scrollY
|
sceneState.scrollY
|
||||||
);
|
);
|
||||||
@ -801,6 +801,9 @@ const someElementIsSelected = () =>
|
|||||||
const ELEMENT_SHIFT_TRANSLATE_AMOUNT = 5;
|
const ELEMENT_SHIFT_TRANSLATE_AMOUNT = 5;
|
||||||
const ELEMENT_TRANSLATE_AMOUNT = 1;
|
const ELEMENT_TRANSLATE_AMOUNT = 1;
|
||||||
|
|
||||||
|
let lastCanvasWidth = -1;
|
||||||
|
let lastCanvasHeight = -1;
|
||||||
|
|
||||||
class App extends React.Component<{}, AppState> {
|
class App extends React.Component<{}, AppState> {
|
||||||
public componentDidMount() {
|
public componentDidMount() {
|
||||||
document.addEventListener("keydown", this.onKeyDown, false);
|
document.addEventListener("keydown", this.onKeyDown, false);
|
||||||
@ -941,6 +944,9 @@ class App extends React.Component<{}, AppState> {
|
|||||||
private removeWheelEventListener: (() => void) | undefined;
|
private removeWheelEventListener: (() => void) | undefined;
|
||||||
|
|
||||||
public render() {
|
public render() {
|
||||||
|
const canvasWidth = window.innerWidth - CANVAS_WINDOW_OFFSET_LEFT;
|
||||||
|
const canvasHeight = window.innerHeight - CANVAS_WINDOW_OFFSET_TOP;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className="container"
|
className="container"
|
||||||
@ -1079,8 +1085,12 @@ class App extends React.Component<{}, AppState> {
|
|||||||
</div>
|
</div>
|
||||||
<canvas
|
<canvas
|
||||||
id="canvas"
|
id="canvas"
|
||||||
width={window.innerWidth - CANVAS_WINDOW_OFFSET_LEFT}
|
style={{
|
||||||
height={window.innerHeight - CANVAS_WINDOW_OFFSET_TOP}
|
width: canvasWidth,
|
||||||
|
height: canvasHeight
|
||||||
|
}}
|
||||||
|
width={canvasWidth * window.devicePixelRatio}
|
||||||
|
height={canvasHeight * window.devicePixelRatio}
|
||||||
ref={canvas => {
|
ref={canvas => {
|
||||||
if (this.removeWheelEventListener) {
|
if (this.removeWheelEventListener) {
|
||||||
this.removeWheelEventListener();
|
this.removeWheelEventListener();
|
||||||
@ -1092,6 +1102,19 @@ class App extends React.Component<{}, AppState> {
|
|||||||
});
|
});
|
||||||
this.removeWheelEventListener = () =>
|
this.removeWheelEventListener = () =>
|
||||||
canvas.removeEventListener("wheel", this.handleWheel);
|
canvas.removeEventListener("wheel", this.handleWheel);
|
||||||
|
|
||||||
|
// Whenever React sets the width/height of the canvas element,
|
||||||
|
// the context loses the scale transform. We need to re-apply it
|
||||||
|
if (
|
||||||
|
canvasWidth !== lastCanvasWidth ||
|
||||||
|
canvasHeight !== lastCanvasHeight
|
||||||
|
) {
|
||||||
|
lastCanvasWidth = canvasWidth;
|
||||||
|
lastCanvasHeight = canvasHeight;
|
||||||
|
canvas
|
||||||
|
.getContext("2d")!
|
||||||
|
.scale(window.devicePixelRatio, window.devicePixelRatio);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onMouseDown={e => {
|
onMouseDown={e => {
|
||||||
@ -1393,8 +1416,4 @@ const canvas = document.getElementById("canvas") as HTMLCanvasElement;
|
|||||||
const rc = rough.canvas(canvas);
|
const rc = rough.canvas(canvas);
|
||||||
const context = canvas.getContext("2d")!;
|
const context = canvas.getContext("2d")!;
|
||||||
|
|
||||||
// Big hack to ensure that all the 1px lines are drawn at 1px instead of 2px
|
|
||||||
// https://stackoverflow.com/questions/13879322/drawing-a-1px-thick-line-in-canvas-creates-a-2px-thick-line/13879402#comment90766599_13879402
|
|
||||||
context.translate(0.5, 0.5);
|
|
||||||
|
|
||||||
ReactDOM.render(<App />, rootElement);
|
ReactDOM.render(<App />, rootElement);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user