我正在继续详细阐述我的小项目并学习javascript& SVG。
在我的上一个代码中,我实现了“单击以生成元素并将其拖动”,方法是创建所单击对象的副本(cloneNode(true)),然后将其重新分配给可拖动对象。
现在我重构了整个代码以利用javascript对象工厂,我似乎一直试图找出正确的“循环”;(
尝试下面的剪辑: - 我设法创建了draggables(库存形状) - 我设法添加一些逻辑来识别它的库存类,并复制和更改类为“draggable” - 现在有两个问题: - 衍生的对象并不是原始Draggables的完整副本,因此它们没有鼠标侦听器,我不知道如何从原始对象中分离侦听器......
你如何编写所有代码?
//var morphUIon = false;
var morphTarget = null;
var canvas = {};
var inventory = {};
canvas = document.getElementById("canvas");
inventory = document.getElementById("inventory");
window.onload = function() {
var src = document.querySelectorAll(".inventory");
for (var h = 0; h < src.length; h++) {
var o = new Draggable(src[h]);
//inventory.push(o);
}
}
function Draggable(elem) {
this.target = elem
this.clickPoint = this.target.ownerSVGElement.createSVGPoint()
this.lastMove = this.target.ownerSVGElement.createSVGPoint()
this.currentMove = this.target.ownerSVGElement.createSVGPoint()
this.dpath = this.target.getAttribute("d")
this.subclass = this.target.getAttribute("subclass")
this.target.pathPoints = parsePathToPoints(this.dpath, this.subclass)
this.target.addEventListener("dblclick", this)
this.target.addEventListener("mousedown", this)
this.handleEvent = function(evt) {
switch (evt.type) {
case 'mousedown':
evt.preventDefault()
if (this.target.classList.contains("inventory")) {
var spawned = this.target.cloneNode(true);
spawned.classList.remove("inventory");
spawned.classList.add("draggable");
canvas.appendChild(spawned);
this.target = spawned;
//return;
}
this.clickPoint = globalToLocalCoords(evt.clientX, evt.clientY)
this.target.classList.add("dragged")
this.target.ownerSVGElement.addEventListener("mousemove", this.move)
this.target.ownerSVGElement.addEventListener("mouseup", this.endMove)
//to ensure dblclick is "heard" we delay the pointer trick below by 200ms.
//setTimeout(function() {this.target.setAttribute("pointer-events", "none")}, 200)
break;
case 'dblclick':
evt.preventDefault()
if (morphTarget) {
if (morphTarget!=this.target) {
morphTarget.classList.remove("morphed")
this.target.classList.add("morphed")
morphTarget = elem;
} else {
return;
}
} else {
morphTarget = elem
this.target.classList.add("morphed")
}
Morph(this.target);
break;
}
}
this.move = function(evt) {
evt.preventDefault()
var p = globalToLocalCoords(evt.clientX, evt.clientY)
this.currentMove.x = this.lastMove.x + (p.x - this.clickPoint.x)
this.currentMove.y = this.lastMove.y + (p.y - this.clickPoint.y)
this.target.setAttribute("transform", "translate(" + this.currentMove.x + "," + this.currentMove.y + ")")
}.bind(this)
this.endMove = function(evt) {
this.lastMove.x = this.currentMove.x
this.lastMove.y = this.currentMove.y
this.target.classList.remove("dragged")
//this.target.setAttribute("pointer-events", "all")
this.target.ownerSVGElement.removeEventListener("mousemove", this.move)
this.target.ownerSVGElement.removeEventListener("mouseup", this.endMove)
}.bind(this)
function globalToLocalCoords(x, y) {
var p = elem.ownerSVGElement.createSVGPoint()
var m = elem.parentNode.getScreenCTM()
p.x = x
p.y = y
return p.matrixTransform(m.inverse())
}
}
function Morph(elem) {
console.log(elem);
}
function parsePathToPoints(d, sub) {
var darray = [];
var pathPoints = {};
switch (sub) {
case 'circle':
darray = d.replace(/M|A|Z/g, "").replace(/,/g, " ").split(" ").map(Number);
pathPoints.p0 = {"x": darray[0], "y": darray[1]};
pathPoints.p1 = {"x": darray[0]+darray[2], "y": darray[1]-darray[3]};
pathPoints.p2 = {"x": darray[7], "y": darray[8]};
pathPoints.p3 = {"x": darray[0]+darray[2], "y": darray[1]+darray[3]};
pathPoints.p4 = {"x": darray[2], "y": darray[3]}; // = radius used in the circle path
return pathPoints;
break;
case 'curve4':
darray = d.replace(/M|Q/g, "").replace(/,/g, " ").split(" ").map(Number);
for (i=0, j=1, o=9; i<darray.length-2; i+=2, j+=2, o++) {
pathPoints["p"+(o-9)] = {"x": darray[i], "y": darray[j]};
};
return pathPoints;
break;
case 'curve3':
darray = d.replace(/M|Q/g, "").replace(/,/g, " ").split(" ").map(Number)
for (i=0, j=1, o=9; i<darray.length; i+=2, j+=2, o++) {
pathPoints["p"+(o-9)] = {"x": darray[i], "y": darray[j]};
}
return pathPoints;
break;
case 'curve2':
darray = d.replace(/M|Q/g, "").replace(/,/g, " ").split(" ").map(Number);
for (i=0, j=1, o=9; i<darray.length; i+=2, j+=2, o++) {
pathPoints["p"+(o-9)] = {"x": darray[i], "y": darray[j]};
}
return pathPoints;
break;
case 'curve1':
darray = d.replace(/M|Q/g, "").replace(/,/g, " ").split(" ").map(Number);
for (i=0, j=1, o=9; i<darray.length; i+=2, j+=2, o++) {
pathPoints["p"+(o-9)] = {"x": darray[i], "y": darray[j]};
}
return pathPoints;
break;
case 'cubic':
darray = d.replace(/M|C/g, "").replace(/,/g, " ").split(" ").map(Number);
for (i=0, j=1, o=9; i<darray.length; i+=2, j+=2, o++) {
pathPoints["p"+(o-9)] = {"x": darray[i], "y": darray[j]};
}
return pathPoints;
break;
}
}
function pointsToPath(pathPoints, pathSubclass) {
console.log("joining "+pathPoints+"for a "+pathSubclass);
}
html, body {
margin: 0;
padding: 0;
border: 0;
overflow:hidden;
background-color: #fff;
}
svg {
position: fixed;
top:0%;
left:0%;
width:100%;
height:100%;
}
.inventory {
fill: transparent;
stroke: black;
cursor: move;
}
.draggable {
fill: transparent;
stroke: blue;
cursor: move;
}
.dragged {
fill: transparent;
stroke: green;
cursor: move;
}
.morphed {
fill: transparent;
stroke: red;
cursor: move;
}
.lines {
stroke: green;
stroke-dasharray: 8,5;
stroke-width: 1;
opacity: 0.5;
}
path
{
stroke-width: 3;
stroke: #000;
stroke-linecap: round;
}
path.fill
{
fill: #3ff;
}
#canvasBackground {
fill: lightgrey;
}
#inventoryBackground {
fill: grey;
}
#morphUIrect {
fill: none;
stroke: blue;
stroke-dasharray: 10,5;
opacity: 0.1;
}
.label {
fill: grey;
}
<body>
<svg id="svg"
height="480"
width="480"
viewbox="0 0 480 580"
preserveAspectRatio="xMinYMax meet"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
>
<rect id="canvasBackground" width="480" height="480" x="0" y="0" pointer-events="all"/>
<rect id="inventoryBackground" width="480" height="100" x="0" y="480" pointer-events="all"/>
<g id="inventory">
<path class="inventory" subclass="circle" d="M30,530 A35,35 1 1,1 100,530 A35,35 1 1,1 30,530" />
<path class="inventory" subclass="curve4" d="M125,500 Q155,490 185,500 Q195,530 185,560 Q155,570 125,560 Q115,530 125,500" />
<path class="inventory" subclass="curve3" d="M245,495 Q275,520 280,560 Q245,570 210,560 Q215,520 245,495" />
<path class="inventory" subclass="curve2" d="M305,515 Q345,475 385,515 Q345,555 305,515"/>
<path class="inventory" subclass="curve1" d="M305,550 Q345,580 385,550"/>
<path class="inventory" subclass="cubic" d="M420,495 C470,530 380,530 425,565"/>
</g>
<g id="canvas">
</g>
<g id="morphUI">
<rect id="morphUIrect" visibility = "visible" x="0" y="0" width="0" height="0"></rect>
<line id="l0" visibility = "hidden" class="lines" x1="0" y1="0" x2="0" y2="0"/>
<line id="l1" visibility = "hidden" class="lines" x1="0" y1="0" x2="0" y2="0"/>
<line id="l2" visibility = "hidden" class="lines" x1="0" y1="0" x2="0" y2="0"/>
<line id="l3" visibility = "hidden" class="lines" x1="0" y1="0" x2="0" y2="0"/>
<line id="l4" visibility = "hidden" class="lines" x1="0" y1="0" x2="0" y2="0"/>
<line id="l5" visibility = "hidden" class="lines" x1="0" y1="0" x2="0" y2="0"/>
<line id="l6" visibility = "hidden" class="lines" x1="0" y1="0" x2="0" y2="0"/>
<line id="l7" visibility = "hidden" class="lines" x1="0" y1="0" x2="0" y2="0"/>
<circle id="0" visibility = "hidden" class="control" cx="0" cy="0" r="8"/>
<circle id="1" visibility = "hidden" class="control" cx="0" cy="0" r="8"/>
<circle id="2" visibility = "hidden" class="control" cx="0" cy="0" r="8"/>
<circle id="3" visibility = "hidden" class="control" cx="0" cy="0" r="8"/>
<circle id="4" visibility = "hidden" class="control" cx="0" cy="0" r="8"/>
<circle id="5" visibility = "hidden" class="control" cx="0" cy="0" r="8"/>
<circle id="6" visibility = "hidden" class="control" cx="0" cy="0" r="8"/>
<circle id="7" visibility = "hidden" class="control" cx="0" cy="0" r="8"/>
<circle id="8" visibility = "hidden" class="control" cx="0" cy="0" r="10" _x="0" _y="0"/>
</g>
</svg>
</body>
答案 0 :(得分:0)
通常最好不要将事件处理程序附加到单个SVG形状元素。如果在拖动时快速移动,然后释放鼠标按钮,则鼠标事件可能发生在您拖动的形状之外。你最终可能会遇到“卡住”的形状。最好将mouseup / mousedown事件附加到根awk -v Interval=5 -v Trig=5 -F '[[:blank:]]*|:' '
{
# using format log
# 157.15.14.19 - - 06 Sep 2016 09:13:10 +0300 "GET /index.php?id=1 HTTP/1.1" 200 16977 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"
# $1 2 3 4 5 6 7 8 9 10 11 ...
ThisTime = $7 * 60 + $8
#if new cycle (so this line is not in the cycle)
if ( ThisTime > ( LastTic + Interval ) ) {
# check and print last cycle hit
for( IP in IPCounts) if ( IPCounts[ IP] > Trig) print LastTime " " IP " : " IPCounts[ IP]
# reset reference
split( "", IPCounts)
LastTime = $4 " " $5 " " $6 " " $7 ":" sprintf( "%2d", ( $8 - ( $8 % Interval) )) ":00"
LastTic = $7 * 60 + ( $8 - ( $8 % Interval) )
}
# add this line to new cycle
IPCounts[ $1]++
}
END {
# print last cycle
for( IP in IPCounts) if ( IPCounts[ IP] > Trig) print LastTime " " IP " : " IPCounts[ IP]
}
' YourFile
# for format of log
# op.g.cc 124.145.36.121 - - [21/Nov/2016:03:38:02 +0800] ==> 172.11.0.238:80 "POST ...
# $1 2 3 4 5 6 7 8 9 10 11 ...
# change:
# $7 by $6, $8 by $7
# LastTime = $5 ":" $6 ":" sprintf( "%2d", ( $7 - ( $7 % Interval) )) ":00 +800]"
# IPCounts[ $2]++
元素,并跟踪您在变量中拖动的元素。这样做的好处是您不必继续从拖动的元素中添加和删除事件处理程序。