Skip to content

Commit

Permalink
Merge branch 'master' into text-new-line-style-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur authored Jan 13, 2024
2 parents e269f09 + 4828a99 commit f6960b7
Show file tree
Hide file tree
Showing 96 changed files with 1,934 additions and 854 deletions.
3 changes: 2 additions & 1 deletion .codesandbox/ci.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"new",
"/.codesandbox/templates/node",
"/.codesandbox/templates/next",
"/.codesandbox/templates/vanilla"
"/.codesandbox/templates/vanilla",
"/.codesandbox/templates/vue"
],
"node": "16"
}
5 changes: 2 additions & 3 deletions .codesandbox/deploy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import match from 'micromatch';
import path from 'path';
import { getGitInfo } from '../scripts/git.mjs';

const BINARY_EXT = ['png', 'jpg', 'jpeg'];
const BINARY_EXT = ['png', 'jpg', 'jpeg', 'ico'];

function bufferToBase64DataUrl(buffer, mimeType) {
return 'data:' + mimeType + ';base64,' + buffer.toString('base64');
Expand Down Expand Up @@ -94,7 +94,6 @@ export async function createCodeSandbox(appPath) {
);
return `https://codesandbox.io/s/${sandbox_id}`;
} catch (error) {
// console.log(error.response.data);
throw new Error(error.response.data);
throw error.response?.data || error;
}
}
100 changes: 42 additions & 58 deletions .codesandbox/templates/next/components/Canvas.tsx
Original file line number Diff line number Diff line change
@@ -1,69 +1,53 @@
import * as fabric from 'fabric';
import React, { useCallback, useEffect, useRef } from 'react';
import React, { useEffect, useRef } from 'react';

const DEV_MODE = process.env.NODE_ENV === 'development';

export function useCanvas(
ref?: React.ForwardedRef<HTMLCanvasElement>,
init?: (canvas: fabric.Canvas) => any,
saveState = false,
deps: any[] = []
) {
const elementRef = useRef<HTMLCanvasElement>(null);
const fc = useRef<fabric.Canvas | null>(null);
const data = useRef<any>(null);
declare global {
var canvas: fabric.Canvas | undefined;
}

export const Canvas = React.forwardRef<
fabric.Canvas,
{ onLoad?(canvas: fabric.Canvas): void }
>(({ onLoad }, ref) => {
const canvasRef = useRef<HTMLCanvasElement>(null);

const setRef = useCallback(
(el: HTMLCanvasElement | null) => {
//@ts-ignore
elementRef.current = el;
ref && (ref.current = elementRef.current);
// save state
if (DEV_MODE && saveState && fc.current) {
data.current = fc.current.toJSON();
}
// dispose canvas
fc.current?.dispose();
// set/clear ref
if (!el) {
fc.current = null;
return;
}
const canvas = new fabric.Canvas(el);
window.canvas = fc.current = canvas;
// invoke callback
init && init(canvas);
// restore state
if (DEV_MODE && saveState && data.current) {
canvas.loadFromJSON(data.current);
}
},
[saveState, ...deps]
);
useEffect(() => {
// disposer
if (!canvasRef.current) {
return;
}

const canvas = new fabric.Canvas(canvasRef.current);

DEV_MODE && (window.canvas = canvas);

if (typeof ref === 'function') {
ref(canvas);
} else if (typeof ref === 'object' && ref) {
ref.current = canvas;
}

// it is crucial `onLoad` is a dependency of this effect
// to ensure the canvas is disposed and re-created if it changes
onLoad?.(canvas);

return () => {
// save state
if (DEV_MODE && saveState && fc.current) {
data.current = fc.current.toJSON();
}
// we avoid unwanted disposing by doing so only if element ref is unavailable
if (!elementRef.current) {
fc.current?.dispose();
fc.current = null;
DEV_MODE && delete window.canvas;

if (typeof ref === 'function') {
ref(null);
} else if (typeof ref === 'object' && ref) {
ref.current = null;
}

// `dispose` is async
// however it runs a sync DOM cleanup
// its async part ensures rendering has completed
// and should not affect react
canvas.dispose();
};
}, [saveState]);
return [fc, setRef] as [typeof fc, typeof setRef];
}
}, [canvasRef, onLoad]);

export const Canvas = React.forwardRef<
HTMLCanvasElement,
{
onLoad?: (canvas: fabric.Canvas) => any;
saveState?: boolean;
}
>(({ onLoad, saveState }, ref) => {
const [canvasRef, setCanvasElRef] = useCanvas(ref, onLoad, saveState);
return <canvas ref={setCanvasElRef} />;
return <canvas ref={canvasRef} />;
});
76 changes: 48 additions & 28 deletions .codesandbox/templates/next/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,60 @@
import * as fabric from 'fabric';
import { NextPage } from 'next';
import { useCallback } from 'react';
import { useRef, useCallback } from 'react';
import { Canvas } from '../components/Canvas';

const IndexPage: NextPage = () => {
const onLoad = useCallback(async (canvas: fabric.Canvas) => {
canvas.setDimensions({
width: window.innerWidth,
height: 500,
});
const textValue = 'fabric.js sandbox';
const text = new fabric.Textbox(textValue, {
originX: 'center',
top: 20,
textAlign: 'center',
styles: fabric.util.stylesFromArray(
[
{
style: {
fontWeight: 'bold',
fontSize: 64,
const ref = useRef<fabric.Canvas>(null);

const onLoad = useCallback(
(canvas: fabric.Canvas) => {
canvas.setDimensions({
width: window.innerWidth,
height: 500,
});
const textValue = 'fabric.js sandbox';
const text = new fabric.Textbox(textValue, {
originX: 'center',
top: 20,
textAlign: 'center',
styles: fabric.util.stylesFromArray(
[
{
style: {
fontWeight: 'bold',
fontSize: 64,
},
start: 0,
end: 9,
},
start: 0,
end: 9,
},
],
textValue
),
});
canvas.add(text);
canvas.centerObjectH(text);
}, []);
],
textValue
),
});
canvas.add(text);
canvas.centerObjectH(text);

const animate = (toState: number) => {
text.animate(
{ scaleX: Math.max(toState, 0.1) * 2 },
{
onChange: () => canvas.renderAll(),
onComplete: () => animate(Number(!toState)),
duration: 1000,
easing: toState
? fabric.util.ease.easeInOutQuad
: fabric.util.ease.easeInOutSine,
}
);
};
animate(1);
},
[ref]
);

return (
<div className="position-relative">
<Canvas onLoad={onLoad} />
<Canvas ref={ref} onLoad={onLoad} />
</div>
);
};
Expand Down
40 changes: 4 additions & 36 deletions .codesandbox/templates/vanilla/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as fabric from 'fabric';
import './styles.css';
import { testCase } from './testcases/simpleTextbox';
// import { testCase } from './testcases/polygons';

const el = document.getElementById('canvas');
const canvas = (window.canvas = new fabric.Canvas(el));
Expand All @@ -9,39 +11,5 @@ canvas.setDimensions({
width: 500,
height: 500,
});
const textValue = 'fabric.js sandbox';
const text = new fabric.Textbox(textValue, {
originX: 'center',
splitByGrapheme: true,
width: 200,
top: 20,
styles: fabric.util.stylesFromArray(
[
{
style: {
fontWeight: 'bold',
fontSize: 64,
},
start: 0,
end: 9,
},
],
textValue
),
});
canvas.add(text);
canvas.centerObjectH(text);
function animate(toState) {
text.animate(
{ scaleX: Math.max(toState, 0.1) * 2 },
{
onChange: () => canvas.renderAll(),
onComplete: () => animate(!toState),
duration: 1000,
easing: toState
? fabric.util.ease.easeInOutQuad
: fabric.util.ease.easeInOutSine,
}
);
}
// animate(1);

testCase(canvas);
Loading

0 comments on commit f6960b7

Please sign in to comment.