Skip to content

Commit

Permalink
feat: support canvans 2d for cache (#6367)
Browse files Browse the repository at this point in the history
* feat: support canvans 2d for cache

* fix: image src hydrate

* chore: update lock

* fix: img not match when ssr

* fix: canvas dispalay

* chore: remove runtime

* chore: add event for taobao

* chore: add window.WindVane

* chore: add declare

* chore: add changeset

* feat: add cacheCanvasToStorage to ref

* chore: modify function

* feat: add @ice/cache-canvas

* feat: replace mounted

* chore: add dependence to useCallback

* chore: add try to Storage

* chore: import CacheCanvas from ice in example

* chore: add changeset

* chore: add conment

* feat: add rendered

* feat: add getSnapshot

* feat: modify

* chore: add import meta

* chore: add CacheCanvasProps
  • Loading branch information
answershuto authored Jul 17, 2023
1 parent cf8a78e commit 018238f
Show file tree
Hide file tree
Showing 25 changed files with 600 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-rockets-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/cache-canvas': patch
---

feat: support cache of 2d cavans
5 changes: 5 additions & 0 deletions .changeset/tough-lobsters-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/plugin-canvas': patch
---

feat: support cache of 2d cavans
2 changes: 2 additions & 0 deletions examples/cavans-project/.browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
defaults
ios_saf 9
10 changes: 10 additions & 0 deletions examples/cavans-project/ice.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from '@ice/app';
import canvasPlugin from '@ice/plugin-canvas';

export default defineConfig(() => ({
plugins: [
canvasPlugin(),
],
ssr: true,
ssg: false,
}));
25 changes: 25 additions & 0 deletions examples/cavans-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@examples/canvas-project",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "ice start",
"build": "ice build"
},
"description": "",
"author": "",
"license": "MIT",
"dependencies": {
"@ice/app": "workspace:*",
"@ice/plugin-canvas": "workspace:*",
"@ice/runtime": "workspace:*",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"@ice/cache-canvas": "workspace:*"
},
"devDependencies": {
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"webpack": "^5.86.0"
}
}
3 changes: 3 additions & 0 deletions examples/cavans-project/src/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineAppConfig } from 'ice';

export default defineAppConfig(() => ({}));
7 changes: 7 additions & 0 deletions examples/cavans-project/src/components/bar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Bar() {
return (
<div>
bar
</div>
);
}
22 changes: 22 additions & 0 deletions examples/cavans-project/src/document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Meta, Title, Links, Main, Scripts } from 'ice';

function Document() {
return (
<html>
<head>
<meta charSet="utf-8" />
<meta name="description" content="ICE Demo" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Title />
<Links />
</head>
<body>
<Main />
<Scripts />
</body>
</html>
);
}

export default Document;
65 changes: 65 additions & 0 deletions examples/cavans-project/src/pages/home.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { definePageConfig, CacheCanvas } from 'ice';
import { useRef } from 'react';
import styles from './index.module.css';

export type RefCacheCanvas = {
cacheCanvasToStorage: () => void;
};

const GAME_CANVAS_ID = 'canvas-id';

export default function Home() {
const childRef = useRef<RefCacheCanvas>();
const initFunc = () => {
return new Promise((resolve) => {
const canvas: HTMLCanvasElement | null = document.getElementById(GAME_CANVAS_ID) as HTMLCanvasElement;
if (canvas && typeof canvas.getContext === 'function') {
let ctx: CanvasRenderingContext2D | null = canvas.getContext('2d');

ctx?.fillRect(25, 25, 100, 100);
ctx?.clearRect(45, 45, 60, 60);
ctx?.strokeRect(50, 50, 50, 50);
}

setTimeout(() => {
console.log('canvas paint ready!');
resolve(true);
}, 5000);
});
};
return (
<>
<h2 className={styles.title}>Home Page</h2>
<CacheCanvas
ref={childRef}
id={GAME_CANVAS_ID}
init={initFunc}
fallback={() => <div>fallback</div>}
/>
<button
style={{ display: 'block' }}
onClick={() => {
console.log('active cache!');
childRef.current?.cacheCanvasToStorage();
}}
>cache canvas</button>
</>
);
}

export const pageConfig = definePageConfig(() => {
return {
title: 'Home',
meta: [
{
name: 'theme-color',
content: '#000',
},
{
name: 'title-color',
content: '#f00',
},
],
auth: ['admin'],
};
});
Binary file added examples/cavans-project/src/pages/ice.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/cavans-project/src/pages/index.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.title {
color: red;
}
3 changes: 3 additions & 0 deletions examples/cavans-project/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface AppData {
title: string;
}
1 change: 1 addition & 0 deletions examples/cavans-project/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="@ice/app/types" />
32 changes: 32 additions & 0 deletions examples/cavans-project/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"compileOnSave": false,
"buildOnSave": false,
"compilerOptions": {
"baseUrl": ".",
"outDir": "build",
"module": "esnext",
"target": "es6",
"jsx": "react-jsx",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"lib": ["es6", "dom"],
"sourceMap": true,
"allowJs": true,
"rootDir": "./",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": false,
"importHelpers": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"skipLibCheck": true,
"paths": {
"@/*": ["./src/*"],
"ice": [".ice"]
}
},
"include": ["src", ".ice", "ice.config.*"],
"exclude": ["build", "public"]
}
46 changes: 46 additions & 0 deletions packages/cache-canvas/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# @ice/cache-canvas

React component for supporting canvas for cache.

## Usage

```bash
npm i @ice/cache-canvas -S
```

```jsx
import MainGame from './game'; // eva.js 的封装

const GAME_CANVAS = 'game-canvas';

export default (props) => {
useEffect(() => {
const gameEl = document.getElementById(GAME_CANVAS);
new MainGame(gameEl, getGameHeight());
}, []);

const init = () => {
return new Promise((resolve) => {
const canvas: HTMLCanvasElement | null = document.getElementById(GAME_CANVAS_ID) as HTMLCanvasElement;
if (canvas && typeof canvas.getContext === 'function') {
let ctx: CanvasRenderingContext2D | null = canvas.getContext('2d');

ctx?.fillRect(25, 25, 100, 100);
ctx?.clearRect(45, 45, 60, 60);
ctx?.strokeRect(50, 50, 50, 50);
}

setTimeout(() => {
console.log('canvas paint ready!');
resolve(true);
}, 5000);
});
}

return (
<>
<CanvasCache id={GAME_CANVAS} useCache={false} init={init} />
</>
);
};
```
29 changes: 29 additions & 0 deletions packages/cache-canvas/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@ice/cache-canvas",
"version": "0.0.8",
"description": "",
"main": "./esm/index.js",
"types": "./esm/index.d.ts",
"scripts": {
"watch": "tsc -w",
"build": "tsc",
"prepublishOnly": "npm run build"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"dependencies": {
"universal-env": "^3.3.3"
},
"peerDependencies": {
"react": "^18",
"react-dom": "^18"
},
"publishConfig": {
"access": "public"
}
}
Loading

0 comments on commit 018238f

Please sign in to comment.