@react-native-ohos/react-native-clippathview 1.1.9-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +15 -0
- package/harmony/clipPath/BuildProfile.ets +6 -0
- package/harmony/clipPath/LICENSE +21 -0
- package/harmony/clipPath/OAT.xml +39 -0
- package/harmony/clipPath/README.OpenSource +11 -0
- package/harmony/clipPath/ResourceTable.txt +0 -0
- package/harmony/clipPath/build-profile.json5 +11 -0
- package/harmony/clipPath/consumer-rules.txt +0 -0
- package/harmony/clipPath/hvigorfile.ts +6 -0
- package/harmony/clipPath/index.ets +15 -0
- package/harmony/clipPath/obfuscation-rules.txt +18 -0
- package/harmony/clipPath/oh-package.json5 +14 -0
- package/harmony/clipPath/src/main/cpp/CMakeLists.txt +7 -0
- package/harmony/clipPath/src/main/cpp/ClipPathProps.h +127 -0
- package/harmony/clipPath/src/main/cpp/ClipPathViewComponentInstance.cpp +102 -0
- package/harmony/clipPath/src/main/cpp/ClipPathViewComponentInstance.h +33 -0
- package/harmony/clipPath/src/main/cpp/ClipPathViewJSIBinder.h +60 -0
- package/harmony/clipPath/src/main/cpp/ClipPathViewNapiBinder.h +79 -0
- package/harmony/clipPath/src/main/cpp/ClipPathViewNoneComponentInstance.cpp +102 -0
- package/harmony/clipPath/src/main/cpp/ClipPathViewNoneComponentInstance.h +38 -0
- package/harmony/clipPath/src/main/cpp/ClipPathViewNoneJSIBinder.h +61 -0
- package/harmony/clipPath/src/main/cpp/ClipPathViewNoneNapiBinder.h +80 -0
- package/harmony/clipPath/src/main/cpp/ClipPathViewNoneNode.cpp +381 -0
- package/harmony/clipPath/src/main/cpp/ClipPathViewNoneNode.h +98 -0
- package/harmony/clipPath/src/main/cpp/ClipPathViewPackage.h +85 -0
- package/harmony/clipPath/src/main/cpp/ComponentDescriptors.h +30 -0
- package/harmony/clipPath/src/main/cpp/ModUtil.cpp +47 -0
- package/harmony/clipPath/src/main/cpp/ModUtil.h +36 -0
- package/harmony/clipPath/src/main/cpp/Props.cpp +95 -0
- package/harmony/clipPath/src/main/cpp/Props.h +108 -0
- package/harmony/clipPath/src/main/cpp/RNCClipPathTurboModule.cpp +38 -0
- package/harmony/clipPath/src/main/cpp/RNCClipPathTurboModule.h +37 -0
- package/harmony/clipPath/src/main/cpp/SVGPathParser.cpp +617 -0
- package/harmony/clipPath/src/main/cpp/SVGPathParser.h +71 -0
- package/harmony/clipPath/src/main/cpp/SVGViewBox.cpp +94 -0
- package/harmony/clipPath/src/main/cpp/SVGViewBox.h +32 -0
- package/harmony/clipPath/src/main/cpp/ShadowNodes.cpp +22 -0
- package/harmony/clipPath/src/main/cpp/ShadowNodes.h +41 -0
- package/harmony/clipPath/src/main/cpp/pen_style_node.h +10 -0
- package/harmony/clipPath/src/main/ets/ClipPathPackage.ts +46 -0
- package/harmony/clipPath/src/main/ets/ClipPathTurboModule.ts +32 -0
- package/harmony/clipPath/src/main/module.json5 +7 -0
- package/harmony/clipPath/src/main/resources/base/element/string.json +8 -0
- package/harmony/clipPath/src/main/resources/en_US/element/string.json +8 -0
- package/harmony/clipPath/src/main/resources/zh_CN/element/string.json +8 -0
- package/harmony/clipPath/ts.ts +26 -0
- package/harmony/clipPath.har +0 -0
- package/index.d.ts +58 -0
- package/index.js +5 -0
- package/package.json +64 -0
- package/react-native-clippath.podspec +30 -0
- package/src/ClipPath.android.js +7 -0
- package/src/ClipPath.ios.js +5 -0
- package/src/ClipPath.web.js +5 -0
- package/src/ClipPathH.android.js +6 -0
- package/src/ClipPathH.ios.js +5 -0
- package/src/ClipPathH.web.js +5 -0
- package/src/ClipPathMobile.js +6 -0
- package/src/ClipPathMobileN.js +6 -0
- package/src/ClipPathNativeComponent.ts +48 -0
- package/src/ClipPathWeb.js +345 -0
- package/src/demo.jpg +0 -0
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import React from "react"
|
|
2
|
+
import { useMemo } from "react"
|
|
3
|
+
import { useRef } from "react"
|
|
4
|
+
import { useLayoutEffect, useState } from "react"
|
|
5
|
+
import { StyleSheet } from "react-native";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const Helper = require('svgpath');
|
|
9
|
+
|
|
10
|
+
var MOS_MEET = 0;
|
|
11
|
+
var MOS_SLICE = 1;
|
|
12
|
+
var MOS_NONE = 2;
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
const SVGViewBox = (vbRect,eRect,align, meetOrSlice)=> {
|
|
16
|
+
// based on https://svgwg.org/svg2-draft/coords.html#ComputingAViewportsTransform
|
|
17
|
+
|
|
18
|
+
// Let vb-x, vb-y, vb-width, vb-height be the min-x, min-y, width and height values of the viewBox attribute respectively.
|
|
19
|
+
var vbX = vbRect.left ;
|
|
20
|
+
var vbY = vbRect.top ;
|
|
21
|
+
var vbX2 = vbRect.right ;
|
|
22
|
+
var vbY2 = vbRect.bottom ;
|
|
23
|
+
var vbWidth = vbX2 - vbX;
|
|
24
|
+
var vbHeight = vbY2 - vbY;
|
|
25
|
+
|
|
26
|
+
// Let e-x, e-y, e-width, e-height be the position and size of the element respectively.
|
|
27
|
+
var eX = eRect.left;
|
|
28
|
+
var eY = eRect.top;
|
|
29
|
+
var eWidth = eRect.right - eRect.left;
|
|
30
|
+
var eHeight = eRect.bottom - eRect.top;;
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
// Initialize scale-x to e-width/vb-width.
|
|
34
|
+
var scaleX = eWidth / vbWidth;
|
|
35
|
+
|
|
36
|
+
// Initialize scale-y to e-height/vb-height.
|
|
37
|
+
var scaleY = eHeight / vbHeight;
|
|
38
|
+
|
|
39
|
+
// Initialize translate-x to e-x - (vb-x * scale-x).
|
|
40
|
+
// Initialize translate-y to e-y - (vb-y * scale-y).
|
|
41
|
+
var translateX = eX - (vbX * scaleX);
|
|
42
|
+
var translateY = eY - (vbY * scaleY);
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
//If align is 'none'
|
|
47
|
+
if (meetOrSlice == MOS_NONE) {
|
|
48
|
+
// Let scale be set the smaller value of scale-x and scale-y.
|
|
49
|
+
// Assign scale-x and scale-y to scale.
|
|
50
|
+
scaleX = scaleY = Math.min(scaleX, scaleY);
|
|
51
|
+
translateX += (eWidth - vbWidth * scaleX) / 2.0;
|
|
52
|
+
translateY += (eHeight - vbHeight * scaleY) / 2.0;
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
} else {
|
|
56
|
+
// If align is not 'none' and meetOrSlice is 'meet', set the larger of scale-x and scale-y to the smaller.
|
|
57
|
+
// Otherwise, if align is not 'none' and meetOrSlice is 'slice', set the smaller of scale-x and scale-y to the larger.
|
|
58
|
+
|
|
59
|
+
if (!(align === "none") && meetOrSlice == MOS_MEET) {
|
|
60
|
+
scaleX = scaleY = Math.min(scaleX, scaleY);
|
|
61
|
+
} else if (!(align === "none") && meetOrSlice == MOS_SLICE) {
|
|
62
|
+
scaleX = scaleY = Math.max(scaleX, scaleY);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// If align contains 'xMid', add (e-width - vb-width * scale-x) / 2 to translate-x.
|
|
66
|
+
if (align.includes("xMid")) {
|
|
67
|
+
translateX += (eWidth - vbWidth * scaleX) / 2.0;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// If align contains 'xMax', add (e-width - vb-width * scale-x) to translate-x.
|
|
71
|
+
if (align.includes("xMax")) {
|
|
72
|
+
translateX += (eWidth - vbWidth * scaleX);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// If align contains 'yMid', add (e-height - vb-height * scale-y) / 2 to translate-y.
|
|
76
|
+
if (align.includes("YMid")) {
|
|
77
|
+
translateY += (eHeight - vbHeight * scaleY) / 2.0;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// If align contains 'yMax', add (e-height - vb-height * scale-y) to translate-y.
|
|
81
|
+
if (align.includes("YMax")) {
|
|
82
|
+
translateY += (eHeight - vbHeight * scaleY);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
// The transform applied to content contained by the element is given by
|
|
88
|
+
// translate(translate-x, translate-y) scale(scale-x, scale-y).
|
|
89
|
+
var result = [0 ,0,0,0]
|
|
90
|
+
result[0] = scaleX;
|
|
91
|
+
result[1] = scaleY;
|
|
92
|
+
result[2] = translateX;
|
|
93
|
+
result[3] = translateY;
|
|
94
|
+
|
|
95
|
+
return result;
|
|
96
|
+
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const viewBoxEvaluator = (value, start,end) => {
|
|
100
|
+
return (value - start) / (end - start);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
const ClipPathWeb = React.forwardRef((props,ref) =>{
|
|
106
|
+
const refAlt = useRef(null)
|
|
107
|
+
const myRef = ref || refAlt
|
|
108
|
+
|
|
109
|
+
const {
|
|
110
|
+
|
|
111
|
+
svgKey,
|
|
112
|
+
d,
|
|
113
|
+
viewBox,
|
|
114
|
+
aspect,
|
|
115
|
+
align,
|
|
116
|
+
|
|
117
|
+
fillRule,
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
strokeWidth,
|
|
121
|
+
strokeStart,
|
|
122
|
+
strokeEnd,
|
|
123
|
+
dashArray,
|
|
124
|
+
strokeCap,
|
|
125
|
+
strokeJoin,
|
|
126
|
+
strokeMiter,
|
|
127
|
+
|
|
128
|
+
translateZ,
|
|
129
|
+
|
|
130
|
+
transX,
|
|
131
|
+
transY,
|
|
132
|
+
transPercentageValue,
|
|
133
|
+
|
|
134
|
+
rot,
|
|
135
|
+
rotO,
|
|
136
|
+
rotOx,
|
|
137
|
+
rotOy,
|
|
138
|
+
rotPercentageValue,
|
|
139
|
+
|
|
140
|
+
sc,
|
|
141
|
+
scX,
|
|
142
|
+
scY,
|
|
143
|
+
scO,
|
|
144
|
+
scOx,
|
|
145
|
+
scOy,
|
|
146
|
+
scPercentageValue,
|
|
147
|
+
|
|
148
|
+
style,
|
|
149
|
+
...rest
|
|
150
|
+
} = props
|
|
151
|
+
|
|
152
|
+
const styleObject = useMemo(()=>{
|
|
153
|
+
if (typeof style === 'number') return StyleSheet.flatten(style)
|
|
154
|
+
if(Array.isArray(style)){
|
|
155
|
+
var styleJs = {}
|
|
156
|
+
style.forEach((v)=>{
|
|
157
|
+
if(typeof v === 'number'){
|
|
158
|
+
let ss = StyleSheet.flatten(style)
|
|
159
|
+
Object.assign(styleJs,ss)
|
|
160
|
+
}else{
|
|
161
|
+
Object.assign(styleJs,v)
|
|
162
|
+
}
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
return styleJs
|
|
166
|
+
}
|
|
167
|
+
return style
|
|
168
|
+
},[style])
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
const userKey = svgKey === undefined ? "" : svgKey
|
|
172
|
+
const path = d === undefined ? "" : d
|
|
173
|
+
const vb = viewBox === undefined ? [0,0,-1,-1] : viewBox
|
|
174
|
+
const asp = aspect === undefined ? "meet" : aspect
|
|
175
|
+
const alg = align === undefined ? "xMidYMid" : align
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
const fr = fillRule === undefined ? "nonzero" : fillRule
|
|
179
|
+
|
|
180
|
+
// const sw = strokeWidth === undefined ? 1 : strokeWidth
|
|
181
|
+
// const cap = strokeCap === undefined ? "butt" : strokeCap
|
|
182
|
+
// const join = strokeJoin === undefined ? "miter" : strokeJoin
|
|
183
|
+
// const miterLimit = strokeMiter === undefined ? 4 : strokeMiter
|
|
184
|
+
|
|
185
|
+
const zIndex = translateZ === undefined ? 0 : translateZ
|
|
186
|
+
|
|
187
|
+
const dx = transX === undefined ? 0 : transX
|
|
188
|
+
const dy = transY === undefined ? 0 : transY
|
|
189
|
+
const transPercent = transPercentageValue === undefined ? false :true
|
|
190
|
+
|
|
191
|
+
const scaleX = sc === undefined ? ( scX === undefined ? 1 : scX) : sc
|
|
192
|
+
const scaleY = sc === undefined ? (scY === undefined ? 1 : scY) : sc
|
|
193
|
+
const scaleOX = scO === undefined ? ( scOx === undefined ? 0 : scOx) : scO
|
|
194
|
+
const scaleOY = scO === undefined ? (scOy === undefined ? 0 : scOy) : scO
|
|
195
|
+
const scalePercent = scPercentageValue === undefined ? false :true
|
|
196
|
+
|
|
197
|
+
const rotation = rot === undefined ? 0 : rot
|
|
198
|
+
const rotationOX = rotO === undefined ? ( rotOx === undefined ? 0 : rotOx) : rotO
|
|
199
|
+
const rotationOY = rotO === undefined ? (rotOy === undefined ? 0 : rotOy) : rotO
|
|
200
|
+
const rotationPercent = rotPercentageValue === undefined ? false :true
|
|
201
|
+
|
|
202
|
+
const [bounds,setbounds] = useState({w:0,h:0})
|
|
203
|
+
|
|
204
|
+
const finalPath = useMemo(()=>{
|
|
205
|
+
|
|
206
|
+
if(path.length > 0 && bounds.w > 0 && bounds.h > 0){
|
|
207
|
+
|
|
208
|
+
var vbLeft = vb[0]
|
|
209
|
+
var vbTop = vb[1]
|
|
210
|
+
var vbWidth = vb[2]
|
|
211
|
+
var vbHeight = vb[3]
|
|
212
|
+
var vbRight = vb[0] + vb[2]
|
|
213
|
+
var vbBottom = vb[1] + vb[3]
|
|
214
|
+
|
|
215
|
+
var baseX = vbWidth / (bounds.w * vbWidth )
|
|
216
|
+
var baseY = vbHeight / (bounds.h * vbHeight )
|
|
217
|
+
|
|
218
|
+
var scVx = 1
|
|
219
|
+
var scVy = 1
|
|
220
|
+
var trVx = 0
|
|
221
|
+
var trVy= 0
|
|
222
|
+
var a = asp === "none" ? 2 : ( asp === "slice" ? 1 : 0)
|
|
223
|
+
|
|
224
|
+
var rectLeft = 0
|
|
225
|
+
var rectTop = 0
|
|
226
|
+
var rectW = bounds.w
|
|
227
|
+
var rectH = bounds.h
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
if(vbWidth >= 0 && vbHeight >= 0){
|
|
231
|
+
var resultVb = SVGViewBox(
|
|
232
|
+
{left: vbLeft , top:vbTop , right:vbRight , bottom: vbBottom},
|
|
233
|
+
{ left: 0 ,top:0,right:bounds.w, bottom:bounds.h },
|
|
234
|
+
alg,
|
|
235
|
+
a,
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
scVx = resultVb[0]
|
|
239
|
+
scVy = resultVb[1]
|
|
240
|
+
trVx = resultVb[2]
|
|
241
|
+
trVy = resultVb[3]
|
|
242
|
+
rectLeft = ( vbLeft * resultVb[0] ) + trVx
|
|
243
|
+
rectTop = ( vbTop * resultVb[1] ) + trVy
|
|
244
|
+
rectW = vbWidth * resultVb[0]
|
|
245
|
+
rectH = vbHeight * resultVb[1]
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
//rotation
|
|
252
|
+
|
|
253
|
+
var rotOriginX = rotationOX
|
|
254
|
+
var rotOriginY = rotationOY
|
|
255
|
+
|
|
256
|
+
if(rotationPercent){
|
|
257
|
+
rotOriginX = rotationOX * bounds.w
|
|
258
|
+
rotOriginY = rotationOY * bounds.h
|
|
259
|
+
}else if(vbWidth >= 0 && vbHeight >= 0){
|
|
260
|
+
rotOriginX = rectLeft + (viewBoxEvaluator(rotationOX,vbLeft,vbRight) * rectW)
|
|
261
|
+
rotOriginY = rectTop + (viewBoxEvaluator(rotationOY,vbTop,vbBottom) * rectH)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
//scale
|
|
265
|
+
|
|
266
|
+
var scOriginX = scaleOX
|
|
267
|
+
var scOriginY = scaleOY
|
|
268
|
+
|
|
269
|
+
if(scalePercent){
|
|
270
|
+
scOriginX = scaleOX * bounds.w
|
|
271
|
+
scOriginY = scaleOY * bounds.h
|
|
272
|
+
}else if(vbWidth >= 0 && vbHeight >= 0){
|
|
273
|
+
scOriginX = rectLeft + (viewBoxEvaluator(scaleOX,vbLeft,vbRight) * rectW)
|
|
274
|
+
scOriginY = rectTop + (viewBoxEvaluator(scaleOY,vbTop,vbBottom) * rectH)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
//translate
|
|
280
|
+
var transPlusX = dx
|
|
281
|
+
var transPlusY = dy
|
|
282
|
+
if(transPercent){
|
|
283
|
+
transPlusX = dx * bounds.w
|
|
284
|
+
transPlusY = dy * bounds.h
|
|
285
|
+
}else if(vb[2] >= 0 && vb[3] >= 0){
|
|
286
|
+
transPlusX = (dx / vbWidth) * rectW
|
|
287
|
+
transPlusY = (dy / vbHeight) * rectH
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
return Helper(path)
|
|
293
|
+
.scale(scVx,scVy)
|
|
294
|
+
.translate(trVx,trVy)
|
|
295
|
+
|
|
296
|
+
.rotate(rotation, rotOriginX ,rotOriginY )
|
|
297
|
+
|
|
298
|
+
.translate(-scOriginX,-scOriginY)
|
|
299
|
+
.scale(scaleX,scaleY)
|
|
300
|
+
.translate(scOriginX,scOriginY)
|
|
301
|
+
|
|
302
|
+
.translate(transPlusX,transPlusY)
|
|
303
|
+
|
|
304
|
+
.scale(baseX,baseY)
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
.toString()
|
|
308
|
+
}
|
|
309
|
+
return "";
|
|
310
|
+
},[bounds,dx,dy,scaleY,scaleX,scaleOX,scaleOY,path,rotation,rotationOX,rotationOY,asp,alg,vb,scalePercent,transPercent,rotationPercent])
|
|
311
|
+
|
|
312
|
+
useLayoutEffect(()=>{
|
|
313
|
+
let w = myRef.current?.clientWidth || 0
|
|
314
|
+
let h = myRef.current?.clientHeight || 0
|
|
315
|
+
if(w !== bounds.w || h !== bounds.h ){
|
|
316
|
+
setbounds({w:w,h:h})
|
|
317
|
+
}
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
const keyClip = `clipPathViewjjfl${userKey}`
|
|
321
|
+
|
|
322
|
+
return(
|
|
323
|
+
<>
|
|
324
|
+
<div {...rest} ref={myRef} style={{...styleObject,clipPath:`${path.length > 0 ? `url(#${keyClip})` : ''}`, zIndex:zIndex}} >
|
|
325
|
+
|
|
326
|
+
</div>
|
|
327
|
+
<svg style={{width:0,height:0}}>
|
|
328
|
+
<defs>
|
|
329
|
+
<clipPath id={keyClip}
|
|
330
|
+
clipPathUnits="objectBoundingBox"
|
|
331
|
+
|
|
332
|
+
>
|
|
333
|
+
<path d={finalPath} fill={"black"} fillRule={fr}
|
|
334
|
+
|
|
335
|
+
/>
|
|
336
|
+
</clipPath>
|
|
337
|
+
</defs>
|
|
338
|
+
</svg>
|
|
339
|
+
|
|
340
|
+
</>
|
|
341
|
+
)
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
export default ClipPathWeb
|
package/src/demo.jpg
ADDED
|
Binary file
|