|
18 | 18 | */ |
19 | 19 |
|
20 | 20 | import type { QuadOptions } from '../../CoreRenderer.js'; |
| 21 | +import type { BorderEffectProps } from '../../webgl/shaders/effects/BorderEffect.js'; |
| 22 | +import type { RadiusEffectProps } from '../../webgl/shaders/effects/RadiusEffect.js'; |
| 23 | +import type { EffectDescUnion } from '../../webgl/shaders/effects/ShaderEffect.js'; |
21 | 24 | import { |
22 | 25 | ROUNDED_RECTANGLE_SHADER_TYPE, |
23 | 26 | UnsupportedShader, |
24 | 27 | } from '../shaders/UnsupportedShader.js'; |
| 28 | +import { formatRgba, parseColorRgba } from './ColorUtils.js'; |
| 29 | + |
| 30 | +type Direction = 'Top' | 'Right' | 'Bottom' | 'Left'; |
25 | 31 |
|
26 | 32 | /** |
27 | 33 | * Extract `RoundedRectangle` shader radius to apply as a clipping |
28 | 34 | */ |
29 | | -export function getRadius(quad: QuadOptions): number { |
| 35 | +export function getRadius(quad: QuadOptions): RadiusEffectProps['radius'] { |
30 | 36 | if (quad.shader instanceof UnsupportedShader) { |
31 | 37 | const shType = quad.shader.shType; |
32 | 38 | if (shType === ROUNDED_RECTANGLE_SHADER_TYPE) { |
33 | 39 | return (quad.shaderProps?.radius as number) ?? 0; |
| 40 | + } else if (shType === 'DynamicShader') { |
| 41 | + const effects = quad.shaderProps?.effects as |
| 42 | + | EffectDescUnion[] |
| 43 | + | undefined; |
| 44 | + |
| 45 | + if (effects) { |
| 46 | + const effect = effects.find((effect: EffectDescUnion) => { |
| 47 | + return effect.type === 'radius' && effect?.props?.radius; |
| 48 | + }); |
| 49 | + |
| 50 | + return (effect && effect.type === 'radius' && effect.props.radius) || 0; |
| 51 | + } |
34 | 52 | } |
35 | 53 | } |
36 | 54 | return 0; |
37 | 55 | } |
| 56 | + |
| 57 | +/** |
| 58 | + * Extract `RoundedRectangle` shader radius to apply as a clipping */ |
| 59 | +export function getBorder( |
| 60 | + quad: QuadOptions, |
| 61 | + direction: '' | Direction = '', |
| 62 | +): BorderEffectProps | undefined { |
| 63 | + if (quad.shader instanceof UnsupportedShader) { |
| 64 | + const shType = quad.shader.shType; |
| 65 | + if (shType === 'DynamicShader') { |
| 66 | + const effects = quad.shaderProps?.effects as |
| 67 | + | EffectDescUnion[] |
| 68 | + | undefined; |
| 69 | + |
| 70 | + if (effects && effects.length) { |
| 71 | + const effect = effects.find((effect: EffectDescUnion) => { |
| 72 | + return ( |
| 73 | + effect.type === `border${direction}` && |
| 74 | + effect.props && |
| 75 | + effect.props.width |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + return effect && effect.props; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return undefined; |
| 85 | +} |
| 86 | + |
| 87 | +export function strokeLine( |
| 88 | + ctx: CanvasRenderingContext2D, |
| 89 | + x: number, |
| 90 | + y: number, |
| 91 | + width: number, |
| 92 | + height: number, |
| 93 | + lineWidth = 0, |
| 94 | + color: number | undefined, |
| 95 | + direction: Direction, |
| 96 | +) { |
| 97 | + if (!lineWidth) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + let sx, |
| 102 | + sy = 0; |
| 103 | + let ex, |
| 104 | + ey = 0; |
| 105 | + |
| 106 | + switch (direction) { |
| 107 | + case 'Top': |
| 108 | + sx = x; |
| 109 | + sy = y; |
| 110 | + ex = width + x; |
| 111 | + ey = y; |
| 112 | + break; |
| 113 | + case 'Right': |
| 114 | + sx = x + width; |
| 115 | + sy = y; |
| 116 | + ex = x + width; |
| 117 | + ey = y + height; |
| 118 | + break; |
| 119 | + case 'Bottom': |
| 120 | + sx = x; |
| 121 | + sy = y + height; |
| 122 | + ex = x + width; |
| 123 | + ey = y + height; |
| 124 | + break; |
| 125 | + case 'Left': |
| 126 | + sx = x; |
| 127 | + sy = y; |
| 128 | + ex = x; |
| 129 | + ey = y + height; |
| 130 | + break; |
| 131 | + } |
| 132 | + ctx.beginPath(); |
| 133 | + ctx.lineWidth = lineWidth; |
| 134 | + ctx.strokeStyle = formatRgba(parseColorRgba(color ?? 0)); |
| 135 | + ctx.moveTo(sx, sy); |
| 136 | + ctx.lineTo(ex, ey); |
| 137 | + ctx.stroke(); |
| 138 | +} |
0 commit comments