Detecting collision and removing child with spritekit in swift 3 - swift3

At the end, I'm trying to detect collision, and delete the marble as it touches the rectangle. How do I do that?
import CoreMotion
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate{
let marbleSprite = SKSpriteNode(imageNamed: "marble")
let marbleSprite1 = SKSpriteNode(imageNamed: "marble")
let marbleSprite2 = SKSpriteNode(imageNamed: "marble")
let marbleSprite3 = SKSpriteNode(imageNamed: "marble")
let marbleSprite4 = SKSpriteNode(imageNamed: "marble")
let marbleSprite5 = SKSpriteNode(imageNamed: "marble")
let marbleSprite6 = SKSpriteNode(imageNamed: "marble")
let marbleSprite7 = SKSpriteNode(imageNamed: "marble")
let marbleSprite8 = SKSpriteNode(imageNamed: "marble")
let marbleSprite9 = SKSpriteNode(imageNamed: "marble")
let rectSprite = SKSpriteNode(imageNamed: "rect")
let rectSprite1 = SKSpriteNode(imageNamed: "rect")
let motionManager = CMMotionManager()
let marbleCategory: UInt32 = 1 << 0
let wallCategory: UInt32 = 2 << 0
let rectCategory: UInt32 = 3 << 0
var teal = SKColor(red: 0, green: 128, blue: 128, alpha: 1)
override func didMove(to view: SKView) {
backgroundColor = teal
marbleSprite.position = CGPoint(x: self.frame.width / 15, y: self.frame.height / 15)
marbleSprite.setScale(0.5)
marbleSprite.physicsBody = SKPhysicsBody(circleOfRadius: marbleSprite.size.width / 2)
marbleSprite.physicsBody?.allowsRotation = true
marbleSprite.physicsBody?.categoryBitMask = marbleCategory
marbleSprite.physicsBody?.collisionBitMask = wallCategory | marbleCategory
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
self.physicsBody?.isDynamic = false
self.physicsBody?.categoryBitMask = wallCategory
marbleSprite1.name = "Marble"
marbleSprite1.position = CGPoint(x: self.frame.width / 15, y: self.frame.height / 15)
marbleSprite1.setScale(0.5)
marbleSprite1.physicsBody = SKPhysicsBody(circleOfRadius: marbleSprite1.size.width / 2)
marbleSprite1.physicsBody?.allowsRotation = true
marbleSprite1.physicsBody?.categoryBitMask = marbleCategory
marbleSprite1.physicsBody?.collisionBitMask = wallCategory | marbleCategory
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
self.physicsBody?.isDynamic = false
self.physicsBody?.categoryBitMask = wallCategory
marbleSprite1.name = "Marble"
marbleSprite2.position = CGPoint(x: self.frame.width / 15, y: self.frame.height / 15)
marbleSprite2.setScale(0.5)
marbleSprite2.physicsBody = SKPhysicsBody(circleOfRadius: marbleSprite2.size.width / 2)
marbleSprite2.physicsBody?.allowsRotation = true
marbleSprite2.physicsBody?.categoryBitMask = marbleCategory
marbleSprite2.physicsBody?.collisionBitMask = wallCategory | marbleCategory
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
self.physicsBody?.isDynamic = false
self.physicsBody?.categoryBitMask = wallCategory
marbleSprite2.name = "Marble"
marbleSprite3.position = CGPoint(x: self.frame.width / 15, y: self.frame.height / 15)
marbleSprite3.setScale(0.5)
marbleSprite3.physicsBody = SKPhysicsBody(circleOfRadius: marbleSprite3.size.width / 2)
marbleSprite3.physicsBody?.allowsRotation = true
marbleSprite3.physicsBody?.categoryBitMask = marbleCategory
marbleSprite3.physicsBody?.collisionBitMask = wallCategory | marbleCategory
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
self.physicsBody?.isDynamic = false
self.physicsBody?.categoryBitMask = wallCategory
marbleSprite3.name = "Marble"
marbleSprite4.position = CGPoint(x: self.frame.width / 15, y: self.frame.height / 15)
marbleSprite4.setScale(0.5)
marbleSprite4.physicsBody = SKPhysicsBody(circleOfRadius: marbleSprite4.size.width / 2)
marbleSprite4.physicsBody?.allowsRotation = true
marbleSprite4.physicsBody?.categoryBitMask = marbleCategory
marbleSprite4.physicsBody?.collisionBitMask = wallCategory | marbleCategory
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
self.physicsBody?.isDynamic = false
self.physicsBody?.categoryBitMask = wallCategory
marbleSprite4.name = "Marble"
marbleSprite5.position = CGPoint(x: self.frame.width / 15, y: self.frame.height / 15)
marbleSprite5.setScale(0.5)
marbleSprite5.physicsBody = SKPhysicsBody(circleOfRadius: marbleSprite5.size.width / 2)
marbleSprite5.physicsBody?.allowsRotation = true
marbleSprite5.physicsBody?.categoryBitMask = marbleCategory
marbleSprite5.physicsBody?.collisionBitMask = wallCategory | marbleCategory
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
self.physicsBody?.isDynamic = false
self.physicsBody?.categoryBitMask = wallCategory
marbleSprite5.name = "Marble"
marbleSprite6.position = CGPoint(x: self.frame.width / 15, y: self.frame.height / 15)
marbleSprite6.setScale(0.5)
marbleSprite6.physicsBody = SKPhysicsBody(circleOfRadius: marbleSprite6.size.width / 2)
marbleSprite6.physicsBody?.allowsRotation = true
marbleSprite6.physicsBody?.categoryBitMask = marbleCategory
marbleSprite6.physicsBody?.collisionBitMask = wallCategory | marbleCategory
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
self.physicsBody?.isDynamic = false
self.physicsBody?.categoryBitMask = wallCategory
marbleSprite6.name = "Marble"
marbleSprite7.position = CGPoint(x: self.frame.width / 15, y: self.frame.height / 15)
marbleSprite7.setScale(0.5)
marbleSprite7.physicsBody = SKPhysicsBody(circleOfRadius: marbleSprite7.size.width / 2)
marbleSprite7.physicsBody?.allowsRotation = true
marbleSprite7.physicsBody?.categoryBitMask = marbleCategory
marbleSprite7.physicsBody?.collisionBitMask = wallCategory | marbleCategory
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
self.physicsBody?.isDynamic = false
self.physicsBody?.categoryBitMask = wallCategory
marbleSprite7.name = "Marble"
marbleSprite8.position = CGPoint(x: self.frame.width / 15, y: self.frame.height / 15)
marbleSprite8.setScale(0.5)
marbleSprite8.physicsBody = SKPhysicsBody(circleOfRadius: marbleSprite8.size.width / 2)
marbleSprite8.physicsBody?.allowsRotation = true
marbleSprite8.physicsBody?.categoryBitMask = marbleCategory
marbleSprite8.physicsBody?.collisionBitMask = wallCategory | marbleCategory
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
self.physicsBody?.isDynamic = false
self.physicsBody?.categoryBitMask = wallCategory
marbleSprite8.name = "Marble"
marbleSprite9.position = CGPoint(x: self.frame.width / 15, y: self.frame.height / 15)
marbleSprite9.setScale(0.5)
marbleSprite9.physicsBody = SKPhysicsBody(circleOfRadius: marbleSprite9.size.width / 2)
marbleSprite9.physicsBody?.allowsRotation = true
marbleSprite9.physicsBody?.categoryBitMask = marbleCategory
marbleSprite9.physicsBody?.collisionBitMask = wallCategory | marbleCategory
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
self.physicsBody?.isDynamic = false
self.physicsBody?.categoryBitMask = wallCategory
marbleSprite9.name = "Marble"
rectSprite.position = CGPoint(x: 0, y: self.frame.height / 2)
rectSprite.setScale(3.0)
rectSprite.physicsBody = SKPhysicsBody(rectangleOf: rectSprite.size)
rectSprite.physicsBody?.allowsRotation = false
rectSprite.physicsBody?.categoryBitMask = rectCategory
rectSprite.physicsBody?.collisionBitMask = marbleCategory
rectSprite.physicsBody?.affectedByGravity = false
rectSprite.physicsBody?.isDynamic = false
rectSprite.name = "Rect"
rectSprite1.position = CGPoint(x: 410, y: self.frame.height / 2)
rectSprite1.setScale(3.0)
rectSprite1.physicsBody = SKPhysicsBody(rectangleOf: rectSprite1.size)
rectSprite1.physicsBody?.allowsRotation = false
rectSprite1.physicsBody?.categoryBitMask = rectCategory
rectSprite1.physicsBody?.collisionBitMask = marbleCategory
rectSprite1.physicsBody?.affectedByGravity = false
rectSprite1.physicsBody?.isDynamic = false
rectSprite1.name = "Rect"
physicsWorld.gravity = CGVector(dx: 0, dy: 0)
addChild(marbleSprite)
addChild(marbleSprite1)
addChild(marbleSprite2)
addChild(marbleSprite3)
addChild(marbleSprite4)
addChild(marbleSprite5)
addChild(marbleSprite6)
addChild(marbleSprite7)
addChild(marbleSprite8)
addChild(marbleSprite9)
addChild(rectSprite)
addChild(rectSprite1)
motionManager.startAccelerometerUpdates()
}
override func update(_ currentTime: TimeInterval) {
if let accelerometerData = motionManager.accelerometerData {
print(accelerometerData)
physicsWorld.gravity = CGVector(dx: accelerometerData.acceleration.x * 10, dy: accelerometerData.acceleration.y * 10)
}
}
func didBegin(_ contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if contact.bodyA.node?.name == "Marble" {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.node?.name == "Marble" && secondBody.node?.name == "Rect" {
secondBody.node?.removeFromParent()
}
}
}
What I thought that would happen was that once the marble touches the rectangle, the marble would disappear, and be gone. What actually happens is when the marble hits the rectangle, nothing happens. Am I doing something wrong?

Try making these changes and set the contact delegate.
let marbleCategory: UInt32 = 0x1 << 0
let wallCategory: UInt32 = 0x1 << 1
let rectCategory: UInt32 = 0x1 << 2
override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
}
func didBegin(_ contact: SKPhysicsContact) {
let collision: UInt32 = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
if collision == marbleCategory | rectCatagory {
rectSprite.removeFromParent()
}
}

Related

render Image not outputting Shiny

output$plot <- renderImage({
outfile <- tempfile(fileext = '.png')
png(outfile, width = 400, height = 300)
venn.diagram(
x = list(
T = T,
I = I
),
main = "Venn Diagram ",
filename =outfile, output=TRUE,
lwd = 2,na = "remove",
fill = c("orange", "blue"),
alpha = c(0.5,0.5),
label.col = "black",
cex=1.5,
fontface = "plain",
cat.col = c("cornflowerblue", "pink"),
cat.cex = 1.5,
cat.fontfamily = "serif",
cat.fontface = "plain",
cat.dist = c(0.05, 0.05),
cat.pos = c(-20, 14),
cat.default.pos = "text",
scaled = FALSE
)
dev.off()
list(src = outfile,
contentType = 'image/png',
width = 400,
height = 300,
alt = "This is alternate text")
}, deleteFile = TRUE)
I was trying plot a venn diagram using this code. But it only displays This is alternate text and not outputting any image on the app, Any Idea ?
Try to create a reactive graph as shown below
output$plot <- renderImage({
vennd <- reactive({venn.diagram(
x = list(
T = T,
I = I
),
main = "Venn Diagram ",
filename =outfile, output=TRUE,
lwd = 2,na = "remove",
fill = c("orange", "blue"),
alpha = c(0.5,0.5),
label.col = "black",
cex=1.5,
fontface = "plain",
cat.col = c("cornflowerblue", "pink"),
cat.cex = 1.5,
cat.fontfamily = "serif",
cat.fontface = "plain",
cat.dist = c(0.05, 0.05),
cat.pos = c(-20, 14),
cat.default.pos = "text",
scaled = FALSE
)
})
outfile <- tempfile(fileext = '.png')
png(outfile, width = 400, height = 300)
vennd()
dev.off()
list(src = outfile,
contentType = 'image/png',
width = 400,
height = 300,
alt = "This is alternate text")
}, deleteFile = TRUE)
output$plot <- renderImage({
vennd <- reactive({venn.diagram(
x = list(
T = T,
I = I
),
main = "",
filename =outfile, output=TRUE,
lwd = 2,na = "remove",imagetype="png",
fill = c("orange", "blue"),
alpha = c(0.5,0.5),
label.col = "black",
cex=1.5,
fontface = "plain",
cat.col = c("cornflowerblue", "pink"),
cat.cex = 1.5,
cat.fontfamily = "serif",
cat.fontface = "plain",
cat.dist = c(0.05, 0.05),
cat.pos = c(-20, 14),
cat.default.pos = "text",
scaled = FALSE
)
})
outfile <- tempfile(fileext = '.png')
png(outfile, width = 500, height = 500,type="cairo")
vennd()
dev.off()
list(src = outfile,
contentType = 'image/png',
width = 500,
height = 500,
alt = "This is alternate text")
}, deleteFile = TRUE)
Need to add imagetype="png" and type="cairo" thank you #YBS

How to leftAnchor multiplier?

I would like to use the for statement to give it left multiplier as many times as 'i'.
How do I write code?
I would definitely like to solve this problem.
Please help me
This is my code
for i in 0 ..< exchangeCategories.count {
let menuBtn: customButton = {
let menu = customButton(type: .system)
menu.item = i
menu.addTarget(self, action: #selector(scrollToPage(_:)), for: .touchUpInside)
menu.translatesAutoresizingMaskIntoConstraints = false
let name = exchangeCategories[i]
let label = UILabel()
label.text = name
label.font = UIFont(name: "SpoqaHanSans-Regular", size: 15)
label.textAlignment = .center
label.textColor = black_textColor
label.backgroundColor = white_backgroundColor
label.translatesAutoresizingMaskIntoConstraints = false
menu.addSubview(label)
label.topAnchor.constraint(equalTo: menu.topAnchor).isActive = true
label.leftAnchor.constraint(equalTo: menu.leftAnchor).isActive = true
label.rightAnchor.constraint(equalTo: menu.rightAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: menu.bottomAnchor, constant: -2).isActive = true
let underLine = UIView()
if i == 0 {
underLine.backgroundColor = selectedPinkColor
label.font = UIFont(name: "SpoqaHanSans-Bold", size: 15)
label.textColor = pink_textColor
}
underLine.tag = i + 1 + 100
underLine.translatesAutoresizingMaskIntoConstraints = false
menu.addSubview(underLine)
underLine.leftAnchor.constraint(equalTo: menu.leftAnchor).isActive = true
underLine.rightAnchor.constraint(equalTo: menu.rightAnchor).isActive = true
underLine.heightAnchor.constraint(equalToConstant: 2).isActive = true
underLine.bottomAnchor.constraint(equalTo: menu.bottomAnchor, constant: 0).isActive = true
return menu
}()
wrap.addSubview(menuBtn)
menuBtn.topAnchor.constraint(equalTo: wrap.topAnchor).isActive = true
menuBtn.leftAnchor.constraint(equalToSystemSpacingAfter: <#T##NSLayoutXAxisAnchor#>, multiplier: <#T##CGFloat#>)
menuBtn.widthAnchor.constraint(equalTo: wrap.widthAnchor, multiplier: 0.25).isActive = true
menuBtn.bottomAnchor.constraint(equalTo: gap.topAnchor).isActive = true
}

Error message given: Generic parameter 'Data' could not be inferred

I have tried many things, but seem to be getting continuous error messages which I cannot remove. The code is listed below, and I have the following questions:
Where is the error? The second ForEach statement has no errors, but the first results in "Generic parameter 'Data' could not be inferred".
Why can't I use a "for" statement within this block? When I do so, I get an error message
It's strange not to define i and j upfront, but when I do so I get error messages. Why?
I would like to make the various (8) circle segments which are drawn buttons. I have found references in older Swift versions (see commented lines below), but how should I create those in SwiftUI?
Any help to this beginner would be much appreciated.
struct ContentView: View {
static let segmentCount = 4
static let circleCount = 2
var body: some View {
let r = CGFloat(75.0)
let center_x = CGFloat(150.0)
let center_y = CGFloat(150.0)
var arc_start = CGFloat(45.0 * CGFloat(Double.pi) / 180.0)
let arc_length = CGFloat(90.0 * CGFloat(Double.pi) / 180.0)
var arc_width = CGFloat(25.0)
let line0Target_x = center_x + r * CGFloat(cos(Double(arc_start)))
let line0Target_y = center_y + r * CGFloat(sin(Double(arc_start)))
let line1Target_x = center_x + (r + arc_width) * CGFloat(cos(Double(arc_start + arc_length)))
let line1Target_y = center_x + (r + arc_width) * CGFloat(sin(Double(arc_start + arc_length)))
ZStack {
ForEach(1..<ContentView.circleCount){ j in
r = CGFloat(25.0 + CGFloat((j) - 1.0) * 25.0)
ForEach(1..<ContentView.segmentCount){ i in
Path { path in
arc_start = CGFloat((45.0 + (CGFloat(i) - 1.0) * 90.0)) * CGFloat(Double.pi) / 180.0
path.move(to: CGPoint(x: line0Target_x, y: line0Target_y))
path.addArc(center: CGPoint(x: center_x, y: center_y), radius: r, startAngle: Angle(radians: Double(arc_start)), endAngle: Angle(radians: Double(arc_start + arc_length)), clockwise: false)
path.addLine(to: CGPoint(x: line1Target_x, y: line1Target_y))
path.addArc(center: CGPoint(x: center_x, y: center_y), radius: (r + arc_width), startAngle: Angle(radians: Double(arc_start + arc_length)), endAngle: Angle(radians: Double(arc_start)), clockwise: true)
path.addLine(to: CGPoint(x: line0Target_x, y: line0Target_y))
path.closeSubpath()
// let shapeLayer = CAShapeLayer()
// shapeLayer.strokeColor = UIColor.red.cgColor
// shapeLayer.fillColor = UIColor.blue.cgColor
// shapeLayer.path = path.cgPath
}
}
}
}
}
}
Your code won't compile, because you are adding lets and vars in body closure, where compiler expects some View.
You can simply move your vars inside of Path closure (or you can make some functions for computing it):
struct ContentView: View {
static let segmentCount = 4
static let circleCount = 2
var body: some View {
ZStack {
ForEach(1..<ContentView.circleCount){ j in
ForEach(1..<ContentView.segmentCount){ i in
Path { path in
let r = CGFloat(25.0 + (CGFloat(j) - 1.0) * 25.0)
let center_x = CGFloat(150.0)
let center_y = CGFloat(150.0)
var arc_start = CGFloat(45.0 * CGFloat(Double.pi) / 180.0)
let arc_length = CGFloat(90.0 * CGFloat(Double.pi) / 180.0)
var arc_width = CGFloat(25.0)
let line0Target_x = center_x + r * CGFloat(cos(Double(arc_start)))
let line0Target_y = center_y + r * CGFloat(sin(Double(arc_start)))
let line1Target_x = center_x + (r + arc_width) * CGFloat(cos(Double(arc_start + arc_length)))
let line1Target_y = center_x + (r + arc_width) * CGFloat(sin(Double(arc_start + arc_length)))
arc_start = CGFloat((45.0 + (CGFloat(i) - 1.0) * 90.0)) * CGFloat(Double.pi) / 180.0
path.move(to: CGPoint(x: line0Target_x, y: line0Target_y))
path.addArc(center: CGPoint(x: center_x, y: center_y), radius: r, startAngle: Angle(radians: Double(arc_start)), endAngle: Angle(radians: Double(arc_start + arc_length)), clockwise: false)
path.addLine(to: CGPoint(x: line1Target_x, y: line1Target_y))
path.addArc(center: CGPoint(x: center_x, y: center_y), radius: (r + arc_width), startAngle: Angle(radians: Double(arc_start + arc_length)), endAngle: Angle(radians: Double(arc_start)), clockwise: true)
path.addLine(to: CGPoint(x: line0Target_x, y: line0Target_y))
path.closeSubpath()
}
}
}
}
}
}

Swift3 - Spritekit Sprite not full size

I want to have a game over screen pop up, using SKSpriteNodes and it scales correctly on first try but from the second, the whole game over is small.
func spawnPlayAgainBTN() {
playAgain = SKSpriteNode(imageNamed: "buttonPlay")
playAgain.position = CGPoint(x: self.frame.midX + 54, y: self.frame.midY - 10)
playAgain.zPosition = 8
playAgain.setScale(0)
self.addChild(playAgain)
playAgain.run(SKAction.scale(to: 0.4, duration: 0.4))
}
func spawnMainMenuBTN() {
mainMenu = SKSpriteNode(imageNamed: "buttonMainMenu ")
mainMenu.position = CGPoint(x: self.frame.midX - 54, y: self.frame.midY - 10)
mainMenu.zPosition = 8
mainMenu.setScale(0)
self.addChild(mainMenu)
mainMenu.run(SKAction.scale(to: 1.0, duration: 0.4))
}
func spawnGameOverScreen() {
gameOverBG = SKSpriteNode(imageNamed: "gameOverBG")
gameOverBG.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
gameOverBG.zPosition = 7
gameOverBG.setScale(0)
self.addChild(gameOverBG)
gameOverBG.run(SKAction.scale(to: 1.0, duration: 0.4))
spawnPlayAgainBTN()
spawnMainMenuBTN()
isRunningGame = false
isShowingGameOver = true
}

jsPlumb connecting raphael element's

I used raphael.js to draw some element's, and now I want to connect them,
how to connect raphael elements with jsplumb ?
For example, in the JSFiddle below : I'm trying to create connections between 2 circles, but I can't, because the circles have no id, my code .
i can't use this :
jsPlumb.connect({source:"id1", target:"id2"})
I finally found a solution to connect raphael elements with jsplumb, I want to thank Mr: # Simon Porritt (jsPlumb creator) for his help: the solution
jsPlumb.ready(function(){
jsPlumb.Defaults.Container = "drawing_board";
// Create a 480 x 640 canvas.
var paper = Raphael('drawing_board');
// of 90 pixels.
var circle1 = paper.circle(140, 110, 90).attr({ fill: '#3D6AA2', stroke: '#000000', 'stroke-width': 8 });
var circle2 = paper.circle(400, 180, 90).attr({ fill: '#3D6AA2', stroke: '#000000', 'stroke-width': 8 });
var rect = paper.rect(50, 280, 90, 70).attr({ fill: '#3D6AA2', stroke: '#000000', 'stroke-width': 8 });
var ellipse = paper.ellipse(300, 420, 90, 70).attr({ fill: '#3D6AA2', stroke: '#000000', 'stroke-width': 8 });
var offsetCalculators = {
"CIRCLE":function(el, parentOffset) {
var cx = parseInt(el.attr("cx"), 10),
cy = parseInt(el.attr("cy"), 10),
r = parseInt(el.attr("r"), 10);
return {
left: parentOffset.left + (cx - r),
top:parentOffset.top + (cy - r)
};
},
"ELLIPSE":function(el, parentOffset) {
var cx = parseInt(el.attr("cx"), 10),
cy = parseInt(el.attr("cy"), 10),
rx = parseInt(el.attr("rx"), 10),
ry = parseInt(el.attr("ry"), 10);
return {
left: parentOffset.left + (cx - rx),
top:parentOffset.top + (cy - ry)
};
},
"RECT":function(el, parentOffset) {
var x = parseInt(el.attr("x"), 10),
y = parseInt(el.attr("y"), 10);
return {
left: parentOffset.left + x,
top:parentOffset.top + y
};
}
};
var sizeCalculators = {
"CIRCLE":function(el) {
var r = parseInt(el.attr("r"), 10);
return [ r * 2, r * 2 ];
},
"ELLIPSE":function(el) {
var rx = parseInt(el.attr("rx"), 10),
ry = parseInt(el.attr("ry"), 10);
return [ rx * 2, ry * 2 ];
},
"RECT":function(el) {
var w = parseInt(el.attr("width"), 10),
h = parseInt(el.attr("height"), 10);
return [ w, h ];
}
};
jsPlumb.CurrentLibrary.getOffset = function(el) {
el = $(el);
var del = el[0], tn = del.tagName.toUpperCase();
if (offsetCalculators[tn]) {
var so = el.parent().offset();
return offsetCalculators[tn](el, so);
}
else
return el.offset();
};
jsPlumb.CurrentLibrary.getSize = function(el) {
el = $(el);
var del = el[0], tn = del.tagName.toUpperCase();
if (sizeCalculators[tn]) {
return sizeCalculators[tn](el);
}
else
return [ el.outerWidth(), el.outerHeight() ];
};
jsPlumb.connect({source:circle1.node, target:circle2.node, anchor:"Center", connector:"Straight"});
jsPlumb.connect({source:circle1.node, target:rect.node, anchors:["Center", "Top"], connector:"Straight"});
jsPlumb.connect({source:circle2.node, target:ellipse.node, anchor:"Center"});
//raphael draggable
var start = function () {
this.ox = this.attr("cx");
this.oy = this.attr("cy");
},
move = function (dx, dy) {
this.attr({cx: this.ox + dx, cy: this.oy + dy});
jsPlumb.repaint(this.node);
},
up = function () { };
paper.set(circle1, circle2).drag(move, start, up); });