feat:形状

This commit is contained in:
2025-08-07 17:36:04 +08:00
parent d3c1a2fd78
commit 738358ab76
17 changed files with 455 additions and 268 deletions
@@ -1,57 +0,0 @@
.shape-card {
position: relative;
width: 300rpx;
height: 300rpx;
border-radius: 16rpx;
// background: #f5f5f5;
background: #ffffff;
border: 1rpx solid rgba(242, 242, 242, 1);
box-shadow: 0rpx 16rpx 24rpx rgba(184, 210, 188, 0.2);
.shape-card-img-wrapper {
padding: 16rpx;
width: 100%;
height: 200rpx;
display: flex;
justify-content: center;
align-items: center;
box-sizing: border-box;
.shape-card-img {
width: 180rpx;
height: 180rpx;
}
}
.shape-card-footer {
margin-inline: 32rpx;
padding-block: 16rpx;
display: flex;
justify-content: start;
align-items: center;
border-top: 1rpx solid #CCCCCC;
.shape-card-footer-color {
width: 60rpx;
height: 60rpx;
border-radius: 50%;
border: 2rpx solid #141414;
margin-right: 34rpx;
}
.shape-card-footer-name {
font-size: 28rpx;
color: #141414;
}
}
.delete-icon {
position: absolute;
top: -18rpx;
right: -18rpx;
.delete-icon-inner {
color: rgba(255, 0, 0, 0.7);
}
}
}
@@ -27,8 +27,8 @@
}
.shape-color {
width: 72rpx;
height: 72rpx;
width: 60rpx;
height: 60rpx;
border-radius: 50%;
border: 1rpx solid #141414;
background-color: skyblue;
@@ -11,7 +11,7 @@ Component({
type: String,
value: '',
},
color: {
fillColor: {
type: String,
value: '',
},
@@ -34,7 +34,7 @@ Component({
* 监听属性变化
*/
observers: {
'svg, color': function () {
'svg, fillColor': function () {
this.generateSvgDataUri();
}
},
@@ -46,12 +46,24 @@ Component({
* 生成完整的 SVG Data URI
*/
generateSvgDataUri() {
const { svg, color } = this.data;
const { svg, fillColor } = this.data;
if (fillColor === 'transparent') return;
if (!svg) return;
// 创建完整的 SVG 字符串,替换填充颜色
const coloredSvg = svg.replace(/fill="#ff0000"/g, `fill="${color || '#ff0000'}"`);
const completeSvg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100" height="100">${coloredSvg}</svg>`;
// 处理 fill 属性:无论原来有没有 fill,都要赋值为 fillColor
let processedSvg = svg;
// 1. 如果有 fill 属性,全部替换为 fillColor
if (/fill="[^"]*"/.test(processedSvg)) {
processedSvg = processedSvg.replace(/fill="[^"]*"/g, `fill="${fillColor || '#ff0000'}"`);
} else {
// 2. 没有 fill 属性,则在第一个标签内加上 fill
processedSvg = processedSvg.replace(
/(<(?:circle|rect|polygon|ellipse|path|line)[^>]*?)(\/?>)/,
`$1 fill="${fillColor || '#ff0000'}"$2`
);
}
const completeSvg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100" height="100">${processedSvg}</svg>`;
// 转换为 Data URI
const svgDataUri = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(completeSvg)}`;
@@ -65,8 +77,13 @@ Component({
this.triggerEvent('onDelete', { key });
},
onColorTap() {
const { key, color } = this.data;
this.triggerEvent('onColorTap', { key, color });
const { key, fillColor } = this.data;
this.triggerEvent('onColorTap', { key, fillColor });
},
onShapeTap() {
const { key } = this.data;
this.triggerEvent('onShapeTap', { key });
},
},
});
@@ -1,9 +1,13 @@
<view class="shape-card">
<image src="{{ svgDataUri }}" class="shape-card-img" mode="aspectFit" />
<image
src="{{ svgDataUri }}"
class="shape-card-img"
mode="aspectFit"
bind:tap="onShapeTap" />
<view class="divider" />
<view
class="shape-color"
style="background-color: {{ color }}"
style="background-color: {{ fillColor }}"
bind:tap="onColorTap" />
<van-icon
class-prefix="toy-icon"