-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 新建canvas组件 * feat: 完成context适配器 * feat: 完成canvas所有绘制 * feat: 添加draw方法的实现 * feat: 为window添加pixelRatio * fix: 修改引用路径 --------- Co-authored-by: mayintao3 <[email protected]>
- Loading branch information
Showing
8 changed files
with
261 additions
and
43 deletions.
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
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
43 changes: 43 additions & 0 deletions
43
packages/taro-platform-harmony/src/components/components-harmony-ets/canvas.ets
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,43 @@ | ||
import type { TaroCanvasElement } from '@tarojs/runtime' | ||
import { cancelAnimationFrame, requestAnimationFrame } from '@tarojs/runtime' | ||
|
||
|
||
@Component | ||
export default struct TaroCanvas { | ||
@ObjectLink node: TaroCanvasElement | ||
rafId: number = 0 | ||
|
||
|
||
aboutToDisappear() { | ||
if(this.rafId) { | ||
cancelAnimationFrame(this.rafId) | ||
} | ||
} | ||
|
||
build() { | ||
Canvas(this.node._context) | ||
.width('100%') | ||
.height('100%') | ||
.backgroundColor('#ffff00') | ||
.onReady(() => { | ||
const context = this.node._context | ||
|
||
const draw = () => { | ||
if (this.node._drawList.length) { | ||
while (this.node._drawList.length) { | ||
const item = this.node._drawList.shift() | ||
if (item) { | ||
if (typeof context[item.key] === 'function') { | ||
context[item.key](...[].concat(item.value)) | ||
} else { | ||
context[item.key] = item.value | ||
} | ||
} | ||
} | ||
} | ||
this.rafId = requestAnimationFrame(draw) | ||
} | ||
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
136 changes: 136 additions & 0 deletions
136
packages/taro-platform-harmony/src/runtime-ets/dom/element/canvas.ts
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,136 @@ | ||
import { eventSource, TaroAny, TaroNode } from '@tarojs/runtime' | ||
|
||
import { TaroElement } from './element' | ||
|
||
import type { CanvasProps, CanvasTouchEvent } from '@tarojs/components/types' | ||
|
||
export class CanvasRenderingContext2DWXAdapter extends CanvasRenderingContext2D { | ||
constructor(settings?: RenderingContextSetting) { | ||
Check failure on line 8 in packages/taro-platform-harmony/src/runtime-ets/dom/element/canvas.ts
|
||
super(settings) | ||
} | ||
|
||
createCircularGradient() { | ||
// Not supported now | ||
} | ||
|
||
draw(cb?: (...args: any[]) => any) { | ||
typeof cb === 'function' && cb() | ||
// Not supported now | ||
} | ||
|
||
setFillStyle(fillStyle: typeof this.fillStyle) { | ||
this.fillStyle = fillStyle | ||
} | ||
|
||
setFontSize(fontSize: number) { | ||
const font = this.font.split(' ') | ||
font[2] = `${fontSize}` | ||
this.font = font.join(' ') | ||
} | ||
|
||
setGlobalAlpha(globalAlpha: typeof this.globalAlpha) { | ||
this.globalAlpha = globalAlpha | ||
} | ||
|
||
setLineCap(lineCap: typeof this.lineCap) { | ||
this.lineCap = lineCap | ||
} | ||
|
||
setLineJoin(lineJoin: typeof this.lineJoin) { | ||
this.lineJoin = lineJoin | ||
} | ||
|
||
setLineWidth(lineWidth: typeof this.lineWidth) { | ||
this.lineWidth = lineWidth | ||
} | ||
|
||
setMiterLimit(miterLimit: typeof this.miterLimit) { | ||
this.miterLimit = miterLimit | ||
} | ||
|
||
setShadow(offsetX: number, offsetY: number, blur: number, color: string) { | ||
this.shadowOffsetX = offsetX | ||
this.shadowOffsetY = offsetY | ||
this.shadowBlur = blur | ||
this.shadowColor = color | ||
} | ||
|
||
setStrokeStyle(strokeStyle: typeof this.strokeStyle) { | ||
this.strokeStyle = strokeStyle | ||
} | ||
|
||
setTextAlign(textAlign: typeof this.textAlign) { | ||
this.textAlign = textAlign | ||
} | ||
|
||
setTextBaseline(textBaseline: typeof this.textBaseline) { | ||
this.textBaseline = textBaseline | ||
} | ||
} | ||
function getContextKey(obj) { | ||
let currentObj = obj | ||
let res = [] | ||
while (currentObj) { | ||
if (currentObj instanceof CanvasRenderingContext2D) { | ||
res = [...res, ...Object.getOwnPropertyNames(currentObj)] | ||
} | ||
currentObj = Object.getPrototypeOf(currentObj) | ||
} | ||
return res | ||
} | ||
|
||
@Observed | ||
export class TaroCanvasElement extends TaroElement<CanvasProps, CanvasTouchEvent> { | ||
_drawList: { | ||
key: string | ||
value: TaroAny | ||
}[] = [] | ||
|
||
settings: RenderingContextSettings | ||
_context: CanvasRenderingContext2D | ||
_contextProxy: CanvasRenderingContext2D | ||
|
||
constructor() { | ||
super('Canvas') | ||
this.settings = new RenderingContextSettings(true) | ||
const context = new CanvasRenderingContext2DWXAdapter(this.settings) as CanvasRenderingContext2D | ||
this._context = context | ||
|
||
const proxyObj = getContextKey(context).reduce((obj, key) => { | ||
if (typeof context[key] === 'function') { | ||
obj[key] = new Proxy(context[key], { | ||
apply: (target, thisArg, argumentsList) => { | ||
this._drawList.push({ | ||
key, | ||
value: argumentsList, | ||
}) | ||
}, | ||
}) | ||
} else { | ||
obj[key] = context[key] | ||
} | ||
return obj | ||
}, {}) | ||
|
||
this._contextProxy = new Proxy(proxyObj, { | ||
set: (_, property, value) => { | ||
this._drawList.push({ | ||
key: property, | ||
value, | ||
}) | ||
return true | ||
}, | ||
}) | ||
} | ||
|
||
get context() { | ||
return this._contextProxy | ||
} | ||
|
||
public setAttribute(name: string, value: TaroAny): void { | ||
if (name === 'canvasId') { | ||
eventSource.set(`canvasId-${value}`, this as TaroNode) | ||
} | ||
super.setAttribute(name, value) | ||
} | ||
} |
Oops, something went wrong.