我有以下的數據結構:拼合點到SVG折線聚合物
'points': [
{
'x': 5535,
'y': 5535
},
{
'x': 5535,
'y': 60000
},
{
'x': 60000,
'y': 60000
},
{
'x': 60000,
'y': 5535
}
];
我想它弄平到5535,5535 5535,60000 60000,60000 60000,5535
作爲polyline
points
屬性來使用。
我在聚合物以下<template>
<polyline
id="polygon",
points="{{points | polygonPoints | toSvgPoints(width, height)}}"
stroke="rgba({{colour.r}}, {{colour.g}}, {{colour.b}}, {{opacity}})"
fill="rgba({{colour.r}}, {{colour.g}}, {{colour.b}}, {{opacity * 0.6}})"
stroke-width="{{lineWidth}}"
stroke-linecap="round"
stroke-linejoin="round"
/>
凡polygonPoints
和toSvgPoints
過濾器看起來像這樣:
/**
* Retrieves the polygon points for this object.
* @param point optionally provide a list of normalized points
* @returns the polygon points or all points if a line
*/
polygonPoints: function(points) {
var array = points.slice(0);
points = points || this.points;
array.push(points[0])
return array;
},
/**
* Retrieves the polygon points for this object.
* @param point optionally provide a list of normalized points
* @returns the polygon points or all points if a line
*/
toSvgPoints: function(points, width, height) {
var i, string = '';
points = points || this.points;
for (i = 0; i < points.length; ++i) {
string += this.normalizedToActual(points[i].x, width);
string += ',';
string += this.normalizedToActual(points[i].y, height);
string += ' ';
}
return string;
},
這個作品中,toSvgPoints
回到正確的點,但在綁定的點值不要通過Polymer自動設置。例如,如果我修改this.points[0].x = 4219
,多邊形不會更新,因爲尚未爲多邊形屬性創建綁定。
如果不提供其他調用重繪的方法,就不能解決這個問題嗎?理想的情況是我只是想這樣做:
<polyline
id="polygon",
points="{{x,y for (points | polygonPoints)}}"
...
/>
這將撲滅x
和y
值在points
屬性和設置綁定。
如何使'toSvgPoints'聽取元素? 'onMutation'是用於輕型DOM元素的,並且不起作用。 –
我添加了一個鏈接觀察-js –