好的,我找到了解决方案。
这是纯网络。
这个想法是使用基本的网络事件“touchstart”和“touchmove”。
并使用该事件来获取手指的坐标。
然后使用函数“document.elementFromPoint(x, y)”获取手指下的元素。
应该可以直接访问“event.target”,但不幸的是,返回的元素始终是“touchstart”事件期间手指下的元素。
然后,我动态更改 CSS。
我们必须使用“DomSanitizer.bypassSecurityTrustStyle()”来执行此操作...如果您在 CSS 中使用来自用户的输入,则不能这样做。就我而言,可能的值位于硬编码数组中。
所以,阅读上面的代码。
变色svg.html
<svg id="colorChanginSVG" version="1.1" (touchstart)="handleMove($event) (touchmove)="handleMove($event)">
<g transform="translate(-24.379463,-109.90178)">
<path
id="pixel"
d="some-path"
[style]=pixelColor />
</g>
</svg>
变色svg.ts
@Component({
selector: 'page-color-changing-svg',
templateUrl: 'color-changing-svg.html',
})
export class ColorChanginSvgPage {
possibleColors: Array<string> = ["#ffffff", "#000000"]
currentFingerColor="#0000ff";
pixelColor:string="fill="+"#ff0000"
constructor(public sanitizer: DomSanitizer) {
}
handleMove(ev) {
let currentElement = document.elementFromPoint(ev.touches[0].pageX, ev.touches[0].pageY);
let id = currentElement.id
if (id === "pixel"){
this.pixelColor = this.sanitizer.bypassSecurityTrustStyle("fill:" + this.currentFingerColor)
}
}
}