Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | 2x 2x 6x 6x 6x 25x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 2x 6x 6x | /* eslint-disable no-redeclare */
'use strict'
const { SLICE } = require('../utils.js')
const regNumber = /[-+]?(?:\d*\.\d+|\d+\.?)(?:[eE][-+]?\d+)?/g
/**
* [generatePathNode 获得替换后的path节点]
* @param {[type]} node [description]
* @return {[type]} [description]
*/
function generatePathNode (node, Doc) {
const newPathNode = Doc.createElement('path')
const slice = Array.prototype.slice
slice.call(node.attributes).forEach(function (attribute) {
newPathNode.setAttribute(attribute.name, attribute.value)
})
return newPathNode
}
function convertPathData (node, Doc) {
Iif (!node.tagName) return
const tagName = String(node.tagName).toLowerCase()
switch (tagName) {
case 'rect':
const x = Number(node.getAttribute('x'))
const y = Number(node.getAttribute('y'))
const width = Number(node.getAttribute('width'))
var height = Number(node.getAttribute('height'))
/*
* rx 和 ry 的规则是:
* 1. 如果其中一个设置为 0 则圆角不生效
* 2. 如果有一个没有设置则取值为另一个
* 3.rx 的最大值为 width 的一半, ry 的最大值为 height 的一半
*/
var rx =
Number(node.getAttribute('rx')) || Number(node.getAttribute('ry')) || 0
var ry =
Number(node.getAttribute('ry')) || Number(node.getAttribute('rx')) || 0
// 非数值单位计算,如当宽度像100%则移除
// if (isNaN(x - y + width - height + rx - ry)) return;
rx = rx > width / 2 ? width / 2 : rx
ry = ry > height / 2 ? height / 2 : ry
// 如果其中一个设置为 0 则圆角不生效
Eif (rx == 0 || ry == 0) {
// var path =
// 'M' + x + ' ' + y +
// 'H' + (x + width) +
// 'V' + (y + height) +
// 'H' + x +
// 'z';
var path =
'M' + x + ' ' + y + 'h' + width + 'v' + height + 'h' + -width + 'z'
} else {
var path =
'M' +
x +
' ' +
(y + ry) +
'a' +
rx +
' ' +
ry +
' 0 0 1 ' +
rx +
' ' +
-ry +
'h' +
(width - rx - rx) +
'a' +
rx +
' ' +
ry +
' 0 0 1 ' +
rx +
' ' +
ry +
'v' +
(height - ry - ry) +
'a' +
rx +
' ' +
ry +
' 0 0 1 ' +
-rx +
' ' +
ry +
'h' +
(rx + rx - width) +
'a' +
rx +
' ' +
ry +
' 0 0 1 ' +
-rx +
' ' +
-ry +
'z'
}
var newPathNode = generatePathNode(node, Doc)
newPathNode.setAttribute('d', path)
;['x', 'y', 'width', 'height', 'rx', 'ry'].forEach(function (attribute) {
newPathNode.removeAttribute(attribute)
})
node.parentNode.replaceChild(newPathNode, node)
break
case 'circle':
var cx = node.getAttribute('cx')
var cy = node.getAttribute('cy')
var r = node.getAttribute('r')
var path =
'M' +
(cx - r) +
' ' +
cy +
'a' +
r +
' ' +
r +
' 0 1 0 ' +
2 * r +
' 0' +
'a' +
r +
' ' +
r +
' 0 1 0 ' +
-2 * r +
' 0' +
'z'
var newPathNode = generatePathNode(node, Doc)
newPathNode.setAttribute('d', path)
;['cx', 'cy', 'r'].forEach(function (attribute) {
newPathNode.removeAttribute(attribute)
})
node.parentNode.replaceChild(newPathNode, node)
break
case 'ellipse':
var cx = node.getAttribute('cx')
var cy = node.getAttribute('cy')
var rx = node.getAttribute('rx')
var ry = node.getAttribute('ry')
Iif (isNaN(cx - cy + rx - ry)) return
var path =
'M' +
(cx - rx) +
' ' +
cy +
'a' +
rx +
' ' +
ry +
' 0 1 0 ' +
2 * rx +
' 0' +
'a' +
rx +
' ' +
ry +
' 0 1 0 ' +
-2 * rx +
' 0' +
'z'
var newPathNode = generatePathNode(node, Doc)
newPathNode.setAttribute('d', path)
;['cx', 'cy', 'rx', 'ry'].forEach(function (attribute) {
newPathNode.removeAttribute(attribute)
})
node.parentNode.replaceChild(newPathNode, node)
break
case 'line':
var x1 = node.getAttribute('x1')
var y1 = node.getAttribute('y1')
var x2 = node.getAttribute('x2')
var y2 = node.getAttribute('y2')
Iif (isNaN(x1 - y1 + (x2 - y2))) {
return
}
var path = 'M' + x1 + ' ' + y1 + 'L' + x2 + ' ' + y2
var newPathNode = generatePathNode(node, Doc)
newPathNode.setAttribute('d', path)
;['x1', 'y1', 'x2', 'y2'].forEach(function (attribute) {
newPathNode.removeAttribute(attribute)
})
node.parentNode.replaceChild(newPathNode, node)
break
case 'polygon':
case 'polyline': // ploygon与polyline是一样的,polygon多边形,polyline折线
var points = (node.getAttribute('points').match(regNumber) || []).map(
Number
)
Iif (points.length < 4) {
return
}
var path =
'M' +
points.slice(0, 2).join(' ') +
'L' +
points.slice(2).join(' ') +
(tagName === 'polygon' ? 'z' : '')
var newPathNode = generatePathNode(node, Doc)
newPathNode.setAttribute('d', path)
newPathNode.removeAttribute('points')
node.parentNode.replaceChild(newPathNode, node)
break
}
}
/**
* transRemoveNode 递归遍历转换基本图形
* @param {*} node
*/
function transNode (node, Doc) {
// 基本的图形都不含有子结点
Eif (!node.hasChildNodes() && node.nodeName.toUpperCase() !== 'PATH') {
convertPathData(node, Doc)
} else if (node.hasChildNodes()) {
SLICE.call(node.childNodes).forEach(function (item) {
transNode(item, Doc)
})
}
}
/**
* convertShapeToPath 基本图形转换
* 包含 rect、cricle、ellipse、line、polygon、polyline
* @param {*} Doc
*/
exports.fn = function convertShapeToPath (Doc) {
SLICE.call(Doc.documentElement.childNodes).forEach(node => {
transNode(node, Doc)
})
}
|