Skip to content

Commit

Permalink
refactor(core): basically developed
Browse files Browse the repository at this point in the history
Co-authored-by: Steven Xiang <wgxh-cli@users.noreply.github.com>
  • Loading branch information
sheepbox8646 and wgxh-cli committed Jun 12, 2024
1 parent f44c584 commit 0385757
Show file tree
Hide file tree
Showing 17 changed files with 216 additions and 186 deletions.
3 changes: 2 additions & 1 deletion examples/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ export class Milestone {
export const defaultScene = new nc.Scene(
new nc.Text('Hello world!', {
x: 100,
y: 100
y: 100,
})
.animate(nc.rippleOut().withAttr({ duration: 3 }))
)

const left = document.querySelector('.left') as HTMLDivElement
Expand Down
2 changes: 1 addition & 1 deletion mods/mod-markdown/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class Markdown extends Widget {
else if (line.match(/( +)?- .+/) || line.match(/( +)? \+.+/) || line.match(/( +)? \* .+/)) {
const listStyle = { fontSize: 16, color: this.textStyle.color }
builder.pushStyle(new ck.TextStyle(listStyle))
parseInline(line.replace(/(.+)?(-|\*|\+)/, '·'), listStyle, ck, builder)
parseInline(line.replace(/(.+)?([\-*+])/, '·'), listStyle, ck, builder)
builder.pop()
}
else {
Expand Down
2 changes: 1 addition & 1 deletion mods/mod-math/src/widgets/numberAxis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Arrow, Line, Text } from '@newcar/basic'
import type { ConvertToProp, Reactive, Ref, WidgetOptions, WidgetStyle } from '@newcar/core'
import type { ConvertToProp, Reactive, WidgetOptions, WidgetStyle } from '@newcar/core'
import { Widget, changed, reactive, ref } from '@newcar/core'
import { Color } from '@newcar/utils'
import type { CanvasKit } from 'canvaskit-wasm'
Expand Down
1 change: 0 additions & 1 deletion mods/mod-math/src/widgets/numberPlane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Arrow, Line, Text } from '@newcar/basic'
import type { ConvertToProp, Reactive, Ref, WidgetOptions, WidgetStyle } from '@newcar/core'
import { Widget, changed, reactive, ref } from '@newcar/core'
import { Color } from '@newcar/utils'
import type { CanvasKit } from 'canvaskit-wasm'
import type { Trend } from './numberAxis'

export interface NumberPlaneOptions extends WidgetOptions {
Expand Down
5 changes: 2 additions & 3 deletions packages/basic/src/animations/creation/stroke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { Figure } from '../../widgets'
/**
* Stroke animation
*/
export const stroke = useAnimate<Figure, { from: number, to: number }>((x) => {
x.widget.style.interval[0] = (x.from ?? 1000) * x.process
x.widget.style.interval[1] = (x.from ?? 1000) * (1 - x.process)
export const stroke = useAnimate<Figure, { from?: number, to?: number }>((x) => {
x.widget.style.interval.value = [(x.from ?? 1000) * x.process, (x.from ?? 1000) * (1 - x.process)]
})
11 changes: 11 additions & 0 deletions packages/basic/src/animations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,14 @@ export * from './color/discolorate'
export * from './movement/move'
export * from './movement/rotate'
export * from './movement/scale'

export * from './creation/create'
export * from './creation/destroy'
export * from './creation/stroke'
export * from './creation/write'

export * from './indicate/circleIndicate'
// export * from './indicate/focusOn'
export * from './indicate/rippleOut'
export * from './indicate/showCreationThenDestructionAround'
// export * from './indicate/showPassingFlashAround'
65 changes: 47 additions & 18 deletions packages/basic/src/animations/indicate/rippleOut.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,56 @@
import { defineAnimation } from '@newcar/core'
import { depend, sequence, withHook } from '@newcar/core'
import type { Widget } from '@newcar/core'
import { Color } from '@newcar/utils'
import { Circle } from '../widgets'
import { scale } from './scale'
import { Circle } from '../../widgets'
import { scale } from '../../'

/**
* Create a circle ripple from the center.
* Input a `origin` to set the most ended radius of the circle.
*/
export const rippleOut = defineAnimation({
act(_widget, _elapsed, _process, _duration, _ck, _params) {},
// eslint-disable-next-line antfu/top-level-function
export const rippleOut = () => {
return withHook<Widget, { origin?: number, color?: Color, transparency?: number }>({
animate() { },

init(widget, startAt, duration, _ck, params) {
widget.add(
new Circle(params.origin ?? 1000, {
before({ widget, duration, origin, color, transparency, by }) {
new Circle(origin ?? 1000, {
style: {
fillColor: params.color ?? Color.WHITE,
transparency: params.transparency ?? 0.3,
fillColor: color ?? Color.WHITE,
transparency: transparency ?? 0.3,
},
}).animate(scale, startAt, duration, {
from: [0, 0],
to: [1, 1],
by: params.by ?? ((x: number) => x),
}).hide(),
)
},
})
}).animate(sequence(
scale.withAttr({
from: [1, 1],
to: [0, 0],
by: by ?? ((x: number) => x),
duration,
}),
depend(c => () => {
c.widget.display.value = false
return true
}),
))

widget.add(
new Circle(origin ?? 1000, {
style: {
fillColor: color ?? Color.WHITE,
transparency: transparency ?? 0.3,
},
}).animate(sequence(
scale.withAttr({
from: [0, 0],
to: [1, 1],
by: by ?? ((x: number) => x),
duration,
}),
depend(c => () => {
c.widget.display.value = false
return true
}),
)),
)
},
})
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
import { defineAnimation } from '@newcar/core'
import { withHook } from '@newcar/core'
import { Color } from '@newcar/utils'
import type { Figure } from '../widgets'
import { Rect } from '../widgets'
import type { Figure } from '../../widgets'
import { Rect } from '../../widgets'

/**
* Create a rectangle that grows and shrinks around the center of the widget
* and then destroys itself.
*/
export const showCreationThenDestructionAround = defineAnimation<Figure>({
act(_widget, _elapsed, process, _duration, _ck, _params) {
this.rect.show()
if (process < 0.5) {
this.rect.style.interval = [this.c * process * 2, this.c - this.c * process * 2]
}
else if (process > 0.5) {
this.rect.style.interval = [this.c * (1 - process) * 2, this.c - this.c * (1 - process) * 2]
}
},
// eslint-disable-next-line antfu/top-level-function
export const showCreationThenDestructionAround = () => {
let rect: Rect
let c: number

init(widget, _startAt, _duration, _ck, params: {
color: Color
width: number
}) {
this.rect = new Rect(Math.abs(widget.range[2] - widget.range[0]) + 20, Math.abs(widget.range[3] - widget.range[1]) + 20, {
x: widget.coordinateParentToChild(widget.range[0], widget.range[1]).x - 10,
y: widget.coordinateParentToChild(widget.range[0], widget.range[1]).y - 10,
style: {
fill: false,
border: true,
borderColor: params.color ?? Color.WHITE,
borderWidth: params.width ?? 2,
},
})
this.c = Math.abs(widget.range[2] - widget.range[0] + 10) * 2
+ Math.abs(widget.range[3] - widget.range[1] + 10) * 2
this.rect.hide()
widget.add(this.rect)
},
return withHook<Figure, { color?: Color, width?: number }>({
animate({ process }) {
rect.show()
if (process < 0.5) {
rect.style.interval.value = [c * process * 2, c - c * process * 2]
}
else if (process > 0.5) {
rect.style.interval.value = [c * (1 - process) * 2, c - c * (1 - process) * 2]
}
},

after(_widget, _elapsed, _ck, _params) {
this.rect.kill()
},
})
before({ widget, color, width }) {
rect = new Rect(Math.abs(widget.range[2] - widget.range[0]) + 20, Math.abs(widget.range[3] - widget.range[1]) + 20, {
x: widget.coordinateParentToChild(widget.range[0], widget.range[1]).x - 10,
y: widget.coordinateParentToChild(widget.range[0], widget.range[1]).y - 10,
style: {
fill: false,
border: true,
borderColor: color ?? Color.WHITE,
borderWidth: width ?? 2,
},
})
c = Math.abs(widget.range[2] - widget.range[0] + 10) * 2
+ Math.abs(widget.range[3] - widget.range[1] + 10) * 2
rect.hide()
widget.add(rect)
},

after() {
rect.remove()
},
})
}
83 changes: 44 additions & 39 deletions packages/basic/src/animations/indicate/showPassingFlashAround.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
import { defineAnimation } from '@newcar/core'
import { Color } from '@newcar/utils'
import { Rect } from '../widgets'
// import { withHook } from '@newcar/core'
// import { Color } from '@newcar/utils'
// import type { Widget } from '@newcar/core'
// import { Rect } from '../../widgets'

/**
* Show a line which pass around the widget.
*/
export const showPassingFlashAround = defineAnimation({
act(_widget, _elapsed, process, _duration, _ck, params) {
this.rect.show()
process = params.by ? params.by(process) : process
this.rect.style.offset = this.c * process
if (process < 0.5)
this.rect.style.interval = [100 * process * 2, this.c - 100 * process * 2]
else if (process > 0.5)
this.rect.style.interval = [100 * (1 - process) * 2, this.c - 100 * (1 - process) * 2]
},
// /**
// * Show a line which pass around the widget.
// */
// export function showPassingFlashAround() {
// let rect: Rect
// let c: number
// return withHook<Widget, {}>({
// animate({ by, process }) {
// rect.show()
// process = by ? by(process) : process
// rect.style.offset.value = c * process
// if (process < 0.5)
// rect.style.interval = [100 * process * 2, this.c - 100 * process * 2]
// else if (process > 0.5)
// this.rect.style.interval = [100 * (1 - process) * 2, this.c - 100 * (1 - process) * 2]
// },

init(widget, _startAt, _duration, _ck, params: {
color: Color
width: number
}) {
this.rect = new Rect(widget.singleRange[2] - widget.singleRange[0] + 20, widget.singleRange[3] - widget.singleRange[1] + 20, {
x: -10,
y: -10,
style: {
fill: false,
border: true,
borderColor: params.color ?? Color.WHITE,
borderWidth: params.width ?? 2,
},
})
this.c = Math.abs(widget.singleRange[2] - widget.singleRange[0] + 10) * 2
+ Math.abs(widget.singleRange[3] - widget.singleRange[1] + 10) * 2
widget.add(this.rect)
this.rect.hide()
},
// init(widget, _startAt, _duration, _ck, params: {
// color: Color
// width: number
// }) {
// this.rect = new Rect(widget.singleRange[2] - widget.singleRange[0] + 20, widget.singleRange[3] - widget.singleRange[1] + 20, {
// x: -10,
// y: -10,
// style: {
// fill: false,
// border: true,
// borderColor: params.color ?? Color.WHITE,
// borderWidth: params.width ?? 2,
// },
// })
// this.c = Math.abs(widget.singleRange[2] - widget.singleRange[0] + 10) * 2
// + Math.abs(widget.singleRange[3] - widget.singleRange[1] + 10) * 2
// widget.add(this.rect)
// this.rect.hide()
// },

after(_widget, _elapsed, _ck, _params) {
this.rect.kill()
},
})
// after(_widget, _elapsed, _ck, _params) {
// this.rect.kill()
// },
// })
// }
12 changes: 8 additions & 4 deletions packages/basic/src/widgets/figure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Figure extends Widget {
this.style.join = ref(options.style.join ?? 'miter')
this.style.cap = ref(options.style.cap ?? 'square')
this.style.offset = ref(options.style.offset ?? 0)
this.style.interval = reactive(options.style.interval ?? [1, 0])
this.style.interval = ref(options.style.interval ?? [1, 0])
}

init(ck: CanvasKit): void {
Expand All @@ -62,7 +62,7 @@ export class Figure extends Widget {
this.strokePaint.setAntiAlias(this.style.antiAlias.value)
try {
const dash = ck.PathEffect.MakeDash(
this.style.interval,
this.style.interval.value,
this.style.offset.value,
)
this.strokePaint.setPathEffect(dash)
Expand Down Expand Up @@ -125,8 +125,12 @@ export class Figure extends Widget {
ck.PathEffect.MakeDash(i, o),
)
}
changed(this.style.offset, offset => makeDashUpdate(this.style.interval, offset.value))
changed(this.style.interval, interval => makeDashUpdate(interval, this.style.offset.value))
changed(this.style.offset, offset => makeDashUpdate(this.style.interval.value, offset.value))
changed(this.style.interval, (interval) => {
this.strokePaint.setPathEffect(
ck.PathEffect.MakeDash(interval.value, this.style.offset.value),
)
})

changed(this.style.blendMode, (blendMode) => {
this.strokePaint.setBlendMode(str2BlendMode(ck, blendMode.value))
Expand Down
6 changes: 2 additions & 4 deletions packages/basic/src/widgets/path.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { Canvas, CanvasKit, Path as ckPath } from 'canvaskit-wasm'
import type { WidgetRange } from '@newcar/core'
import { str2BlendMode, str2StrokeCap, str2StrokeJoin } from '@newcar/utils'
import { changed } from '@newcar/core'
import type { FigureOptions, FigureStyle } from './figure'
import { Figure } from './figure'

Expand Down Expand Up @@ -65,10 +63,10 @@ export class Path extends Figure {
}

draw(canvas: Canvas): void {
if (this.style.border)
if (this.style.border.value)
canvas.drawPath(this.path, this.strokePaint)

if (this.style.fill)
if (this.style.fill.value)
canvas.drawPath(this.path, this.fillPaint)
}

Expand Down
Loading

0 comments on commit 0385757

Please sign in to comment.