Skip to content

Commit

Permalink
Merge pull request #48 from deco-finter/fix/fit-canvas-screen-mobile
Browse files Browse the repository at this point in the history
[FAB-118] Fix canvas fit on mobile screen
  • Loading branch information
daystram committed Sep 25, 2021
2 parents 55905b8 + a378d8c commit 32b4feb
Show file tree
Hide file tree
Showing 8 changed files with 521 additions and 463 deletions.
23 changes: 13 additions & 10 deletions fableous-fe/src/components/canvas/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
scaleUpXY,
translateXY,
} from "./helpers";
import { ASPECT_RATIO, SCALE, SELECT_PADDING } from "./constants";
import { SCALE, SELECT_PADDING } from "./constants";
import { Cursor } from "./CursorScreen";
import { ImperativeCanvasRef, TextShape, TextShapeMap } from "./data";
import { ControllerRole, ToolMode, WSMessageType } from "../../constant";
Expand All @@ -41,8 +41,9 @@ interface CanvasProps {
pageNum: number;
isGallery?: boolean;
isShown?: boolean;
offsetWidth: number;
offsetHeight: number;
onDraw?: () => void;
offsetWidth?: number;
setCursor?: React.Dispatch<Cursor | undefined>;
textShapes: TextShapeMap;
setTextShapes: React.Dispatch<React.SetStateAction<TextShapeMap>>;
Expand All @@ -60,7 +61,6 @@ const defaultProps = {
isGallery: false,
isShown: true,
onDraw: () => {},
offsetWidth: 0,
setCursor: undefined,
toolColor: "#000000ff",
toolMode: ToolMode.None,
Expand All @@ -86,8 +86,9 @@ const Canvas = forwardRef<ImperativeCanvasRef, CanvasProps>(
pageNum,
isGallery,
isShown,
offsetWidth,
offsetHeight,
onDraw = defaultProps.onDraw,
offsetWidth = defaultProps.offsetWidth,
setCursor,
setTextShapes,
textShapes,
Expand Down Expand Up @@ -796,9 +797,8 @@ const Canvas = forwardRef<ImperativeCanvasRef, CanvasProps>(

const adjustCanvasSize = useCallback(() => {
const canvas = canvasRef.current;
const canvasOffsetWidth = canvas.offsetWidth;
canvas.width = canvasOffsetWidth * SCALE;
canvas.height = canvas.width * ASPECT_RATIO;
canvas.width = canvas.offsetWidth * SCALE;
canvas.height = canvas.offsetHeight * SCALE;
}, [canvasRef]);

// exposes callbacks to parent, to be used by toolbar
Expand Down Expand Up @@ -911,10 +911,12 @@ const Canvas = forwardRef<ImperativeCanvasRef, CanvasProps>(

return (
<div
className="place-self-center"
className="relative place-self-center"
style={{
width: offsetWidth,
height: offsetWidth * ASPECT_RATIO,
// -1 so height can shrink
height: offsetHeight - 1,
maxHeight: "100%",
}}
>
<canvas
Expand All @@ -936,7 +938,8 @@ const Canvas = forwardRef<ImperativeCanvasRef, CanvasProps>(
}}
style={{
position: "absolute",
width: offsetWidth,
width: "100%",
height: "100%",
borderRadius: "24px",
// allows onPointerMove to be fired continuously on touch,
// else will be treated as pan gesture leading to short strokes
Expand Down
22 changes: 12 additions & 10 deletions fableous-fe/src/components/canvas/CursorScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* eslint-disable no-case-declarations */
import { useEffect, useRef } from "react";
import { scaleUpXY } from "./helpers";
import { ASPECT_RATIO, SCALE } from "./constants";
import { SCALE } from "./constants";
import { ToolMode } from "../../constant";

export interface Cursor {
Expand All @@ -18,11 +18,12 @@ interface CursorScreenProps {
cursor: Cursor | undefined;
name?: string;
isShown?: boolean;
offsetWidth?: number;
offsetWidth: number;
offsetHeight: number;
}

const CursorScreen = (props: CursorScreenProps) => {
const { cursor, name, isShown, offsetWidth = 0 } = props;
const { cursor, name, isShown, offsetWidth, offsetHeight } = props;
const canvasRef = useRef<HTMLCanvasElement>(document.createElement("canvas"));
const cursorRef = useRef<Cursor>(cursor as Cursor);
cursorRef.current = cursor as Cursor;
Expand Down Expand Up @@ -93,9 +94,8 @@ const CursorScreen = (props: CursorScreenProps) => {
// set canvas size onmount and when canvas appears or becomes hidden
useEffect(() => {
const canvas = canvasRef.current;
const canvasOffsetWidth = canvas.offsetWidth;
canvas.width = canvasOffsetWidth * SCALE;
canvas.height = canvas.width * ASPECT_RATIO;
canvas.width = canvas.offsetWidth * SCALE;
canvas.height = canvas.offsetHeight * SCALE;
const ctx = canvas.getContext("2d");
if (ctx) {
ctx.textBaseline = "middle";
Expand All @@ -110,18 +110,21 @@ const CursorScreen = (props: CursorScreenProps) => {

return (
<div
className="place-self-center"
className="relative place-self-center"
style={{
width: offsetWidth,
height: offsetWidth * ASPECT_RATIO,
// -1 so height can shrink
height: offsetHeight - 1,
maxHeight: "100%",
}}
>
<canvas
ref={canvasRef}
style={{
position: "absolute",
borderRadius: "24px",
width: offsetWidth,
width: "100%",
height: "100%",
touchAction: "none",
msTouchAction: "none",
msTouchSelect: "none",
Expand All @@ -139,7 +142,6 @@ const CursorScreen = (props: CursorScreenProps) => {
CursorScreen.defaultProps = {
name: "",
isShown: true,
offsetWidth: 0,
};

export default CursorScreen;
6 changes: 2 additions & 4 deletions fableous-fe/src/components/canvas/LayerToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ export default function LayerToolbar(props: {
const classes = useStyles();

return (
<div
className={`h-full flex flex-col justify-center items-center ${classes.hideScrollbar}`}
>
<div className="h-full flex flex-col justify-center items-center">
<div
className="overflow-y-scroll overflow-x-hidden"
className={`overflow-y-scroll overflow-x-hidden ${classes.hideScrollbar}`}
style={{
height: offsetHeight || "100%",
maxHeight: "100%",
Expand Down
9 changes: 8 additions & 1 deletion fableous-fe/src/components/canvas/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { MutableRefObject } from "react";
import { SCALE } from "./constants";

export const getCurrentScaling = (
canvasRef: MutableRefObject<HTMLCanvasElement>
): number => {
return canvasRef.current.width / canvasRef.current.offsetWidth;
};

export const translateXY = (
canvasRef: MutableRefObject<HTMLCanvasElement>,
x: number,
y: number
) => {
const bound = canvasRef.current.getBoundingClientRect();
return [(x - bound.x) * SCALE, (y - bound.y) * SCALE];
const currentScale = getCurrentScaling(canvasRef);
return [(x - bound.x) * currentScale, (y - bound.y) * currentScale];
};

// wrap with useCallback() because dependency chain leads to being used at useEffect()
Expand Down
Loading

0 comments on commit 32b4feb

Please sign in to comment.