-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support canvas universal api #104
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6bc8152
feat: support canvas universal api
SoloJiang 9e29bf3
chore: typo
SoloJiang d6d35aa
chore: typo
SoloJiang 7127aa7
chore: supplement types
SoloJiang 6bc9c0a
chore: supplement types
SoloJiang 7b07532
chore: typo
SoloJiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# universal-canvas [![npm](https://img.shields.io/npm/v/universal-canvas.svg)](https://www.npmjs.com/package/universal-canvas) | ||
|
||
一个适配多端的 canvas api。 | ||
|
||
## 支持 | ||
|
||
**icon_miniapp_mp** **icon_miniapp_wx** **icon_web** | ||
|
||
## 安装 | ||
|
||
```bash | ||
$ npm install universal-canvas --save | ||
``` | ||
|
||
## 使用 | ||
|
||
```js | ||
import { createElement, useEffect, Fragment } from 'rax'; | ||
import { createContext } from 'universal-canvas'; | ||
|
||
function App() { | ||
useEffect({ | ||
createContext('canvas', '2d').then(context => { | ||
context.fillStyle = 'red'; | ||
context.fillRect(0, 0, 100, 100); | ||
context.draw(); | ||
}); | ||
}, []); | ||
|
||
return (<> | ||
<canvas id='canvas'></canvas> | ||
</>) | ||
} | ||
``` | ||
|
||
## 方法 | ||
|
||
### `createContext(canvasId, type, options)` | ||
|
||
只有在阿里系小程序中,返回的 `context` 中才含有有实际作用的 `draw` 方法。 | ||
|
||
```js | ||
createContext("canvas").then((context) => { | ||
context.fillStyle = 'red'; | ||
context.fillRect(0, 0, 100, 100); | ||
// 在除了阿里小程序的其它容器中,draw 方法是一个空函数 | ||
context.draw(); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# universal-canvas [![npm](https://img.shields.io/npm/v/universal-canvas.svg)](https://www.npmjs.com/package/universal-canvas) | ||
|
||
Universal canvas api. | ||
|
||
## Support | ||
|
||
**icon_miniapp_mp** **icon_miniapp_wx** **icon_web** | ||
|
||
## Install | ||
|
||
```bash | ||
$ npm install universal-canvas --save | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
import { createElement, useEffect, Fragment } from 'rax'; | ||
import { createContext } from 'universal-canvas'; | ||
|
||
function App() { | ||
useEffect({ | ||
createContext('canvas', '2d').then(context => { | ||
context.fillStyle = 'red'; | ||
context.fillRect(0, 0, 100, 100); | ||
context.draw(); | ||
}); | ||
}, []); | ||
|
||
return (<> | ||
<canvas id='canvas'></canvas> | ||
</>) | ||
} | ||
``` | ||
|
||
## Methods | ||
|
||
### `createContext(canvasId, type, options)` | ||
|
||
Only in MiniApp, the return `context` has valid `draw` method. | ||
|
||
```js | ||
createContext("canvas").then((context) => { | ||
context.fillStyle = 'red'; | ||
context.fillRect(0, 0, 100, 100); | ||
// Only in alibaba miniapp, draw isn't an empty function | ||
context.draw(); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"plugins": [ | ||
[ | ||
"build-plugin-rax-component", | ||
{ | ||
"type": "rax", | ||
"targets": ["web"] | ||
fyangstudio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { createElement, useEffect, render } from 'rax'; | ||
import UniversalDriver from 'driver-universal'; | ||
import View from 'rax-view'; | ||
import { createContext } from '../src/index'; | ||
|
||
function App() { | ||
useEffect(() => { | ||
createContext('canvas', '2d').then((canvasContext) => { | ||
canvasContext.fillStyle = 'red'; | ||
canvasContext.fillRect(0, 0, 100, 100); | ||
// Only valid in miniapp | ||
canvasContext.draw(); | ||
}); | ||
}, []); | ||
return ( | ||
<View> | ||
<canvas id="canvas" width="400" height="400" /> | ||
</View> | ||
); | ||
} | ||
|
||
render(<App />, document.body, { driver: UniversalDriver }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# universal-canvas [![npm](https://img.shields.io/npm/v/universal-canvas.svg)](https://www.npmjs.com/package/universal-canvas) | ||
|
||
一个适配多端的 canvas api。 | ||
|
||
## 支持 | ||
|
||
**icon_miniapp_mp** **icon_miniapp_wx** **icon_web** | ||
|
||
## 安装 | ||
|
||
```bash | ||
$ npm install universal-canvas --save | ||
``` | ||
|
||
## 使用 | ||
|
||
```js | ||
import { createElement, useEffect, Fragment } from 'rax'; | ||
import { createContext } from 'universal-canvas'; | ||
|
||
function App() { | ||
useEffect({ | ||
createContext('canvas', '2d').then(context => { | ||
context.fillStyle = 'red'; | ||
context.fillRect(0, 0, 100, 100); | ||
context.draw(); | ||
}); | ||
}, []); | ||
|
||
return (<> | ||
<canvas id='canvas'></canvas> | ||
</>) | ||
} | ||
``` | ||
|
||
## 方法 | ||
|
||
### `createContext(canvasId, type, options)` | ||
|
||
只有在阿里系小程序中,返回的 `context` 中才含有有实际作用的 `draw` 方法。 | ||
|
||
```js | ||
createContext("canvas").then((context) => { | ||
context.fillStyle = 'red'; | ||
context.fillRect(0, 0, 100, 100); | ||
// 在除了阿里小程序的其它容器中,draw 方法是一个空函数 | ||
context.draw(); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# universal-canvas [![npm](https://img.shields.io/npm/v/universal-canvas.svg)](https://www.npmjs.com/package/universal-canvas) | ||
|
||
Universal canvas api. | ||
|
||
## Support | ||
|
||
**icon_miniapp_mp** **icon_miniapp_wx** **icon_web** | ||
|
||
## Install | ||
|
||
```bash | ||
$ npm install universal-canvas --save | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
import { createElement, useEffect, Fragment } from 'rax'; | ||
import { createContext } from 'universal-canvas'; | ||
|
||
function App() { | ||
useEffect({ | ||
createContext('canvas', '2d').then(context => { | ||
context.fillStyle = 'red'; | ||
context.fillRect(0, 0, 100, 100); | ||
context.draw(); | ||
}); | ||
}, []); | ||
|
||
return (<> | ||
<canvas id='canvas'></canvas> | ||
</>) | ||
} | ||
``` | ||
|
||
## Methods | ||
|
||
### `createContext(canvasId, type, options)` | ||
|
||
Only in MiniApp, the return `context` has valid `draw` method. | ||
|
||
```js | ||
createContext("canvas").then((context) => { | ||
context.fillStyle = 'red'; | ||
context.fillRect(0, 0, 100, 100); | ||
// Only in alibaba miniapp, draw isn't an empty function | ||
context.draw(); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "universal-canvas", | ||
"author": "Rax Team", | ||
"version": "1.0.0", | ||
"description": "Universal canvas api", | ||
"main": "lib/index.js", | ||
"files": [ | ||
"src", | ||
"lib", | ||
"dist" | ||
], | ||
"scripts": { | ||
"start": "build-scripts start", | ||
"build": "build-scripts build" | ||
}, | ||
"pre-commit": ["lint"], | ||
"keywords": [ | ||
"Rax", | ||
"canvas" | ||
], | ||
"engines": { | ||
"npm": ">=3.0.0" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^3.7.5", | ||
"driver-universal": "^3.0.0", | ||
"rax": "^1.1.0", | ||
"rax-text": "^1.0.1", | ||
"rax-view": "^1.0.2", | ||
"build-plugin-rax-component": "^0.2.0", | ||
"@alib/build-scripts": "^0.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { CanvasContext } from '../types' | ||
|
||
declare const my: any; | ||
|
||
function createContext(selector: string): Promise<CanvasContext> { | ||
return new Promise((resolve) => { | ||
const context = my.createCanvasContext(selector); | ||
Object.defineProperty(context, 'fillStyle', { | ||
get() { | ||
return context.setFillStyle; | ||
}, | ||
set(value) { | ||
context.setFillStyle(value); | ||
}, | ||
}); | ||
resolve(context); | ||
}); | ||
} | ||
|
||
export default { | ||
createContext, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { isWeb, isMiniApp, isWeChatMiniProgram } from 'universal-env'; | ||
import aliMiniApp from './ali-miniapp'; | ||
import wechatMiniProgram from './wechat-miniprogram'; | ||
import web from './web'; | ||
import { Canvas } from './types'; | ||
|
||
let canvas: Canvas; | ||
|
||
if (isMiniApp && !isWeb) { | ||
// For cased that import wechat or miniapp sdk in web | ||
canvas = aliMiniApp; | ||
} else if (isWeChatMiniProgram && !isWeb) { | ||
canvas = wechatMiniProgram; | ||
} else { | ||
// Web as default | ||
canvas = web; | ||
} | ||
|
||
const createContext = canvas.createContext; | ||
|
||
export { | ||
createContext | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export type CanvasContext = { draw?: () => void } & RenderingContext; | ||
export interface Canvas { | ||
createContext: ( | ||
selector: string, | ||
type: string, | ||
options: object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个 object 最好 interface 再描述下 |
||
) => Promise<CanvasContext>; | ||
} | ||
|
||
export interface ContextAttributes { | ||
antialias?: boolean, | ||
depth?: boolean, | ||
alpha?: boolean, | ||
willReadFrequently?: boolean, | ||
storage?: string, | ||
failIfMajorPerformanceCaveat?: boolean, | ||
powerPreference?: string, | ||
premultipliedAlpha?: string, | ||
preserveDrawingBuffer?: string, | ||
stencil?: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { CanvasContext, ContextAttributes } from '../types' | ||
|
||
function createContext( | ||
selector: string, | ||
type: string = '2d', | ||
options: ContextAttributes = {} | ||
): Promise<CanvasContext> { | ||
return new Promise((resolve, reject) => { | ||
const canvasNode: HTMLCanvasElement = document.getElementById( | ||
selector | ||
) as HTMLCanvasElement; | ||
if (!canvasNode) reject('The canvas node may not exist.'); | ||
const context :CanvasContext = canvasNode.getContext(type, options); | ||
// For fallback | ||
context.draw = function() {}; | ||
resolve(context); | ||
}); | ||
} | ||
|
||
export default { | ||
createContext, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { CanvasContext, ContextAttributes } from '../types' | ||
|
||
declare const wx: any; | ||
|
||
function createContext( | ||
selector: string, | ||
type: string = '2d', | ||
options: ContextAttributes = {} | ||
): Promise<CanvasContext> { | ||
return new Promise((resolve, reject) => { | ||
const query = wx.createSelectorQuery(); | ||
query | ||
.select(`#${selector}`) | ||
.fields({ node: true, size: true }) | ||
.exec((res) => { | ||
if (!res[0] || !res[0].node) reject('The canvas node may not exist.'); | ||
const canvasNode :HTMLCanvasElement = res[0].node; | ||
const context :CanvasContext = canvasNode.getContext(type, options); | ||
// For fallback | ||
context.draw = function() {}; | ||
resolve(context); | ||
}); | ||
}); | ||
} | ||
|
||
export default { | ||
createContext, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
id 名称改一下,避免误导用户