// create a short variable name to represent the mouse position var mouse = smartShape.currentMousePos; // create a short variable name to represent the control points array var cps = smartShape.elem.controlPoints; // create a new generic object to contain event handler functions operation = new Object(); // event handlers: // define both a BeginDragInsert event handler and an InsertSmartShapeAt event // handler that will create the shape for either of the events depending on how // the user adds the shape to the document (click and release or drag as tool) operation.BeginDragInsert = operation.InsertSmartShapeAt = function(){ // create new path as first element in elements array smartShape.elem.elements[0] = new Path(); // create new contour as first contour in contours array smartShape.elem.elements[0].contours[0] = new Contour(); // set the contour to be a closed contour smartShape.elem.elements[0].contours[0].isClosed = true; // assign a short variable to represent the nodes array var nods = smartShape.elem.elements[0].contours[0].nodes; // create 4 nodes in the contour by altering the nodes array (nods) length nods.length = 4; // set the position of the 4 nodes in the first contour in a box shape // each point is based around a zero location and then has added to it // the position of the mouse using AddPoints. This makes the position of // the points around the position of the mouse. SetNodePosition(nods[0], AddPoints(mouse, {x:-25, y:-25})); SetNodePosition(nods[1], AddPoints(mouse, {x:25, y:-25})); SetNodePosition(nods[2], AddPoints(mouse, {x:25, y:25})); SetNodePosition(nods[3], AddPoints(mouse, {x:-25, y:25})); // create new contour as second contour in contours array smartShape.elem.elements[0].contours[1] = new Contour(); // set the contour to be a closed contour smartShape.elem.elements[0].contours[1].isClosed = true; // assign a short variable to represent the nodes array nods = smartShape.elem.elements[0].contours[1].nodes; // create 4 nodes in the contour by altering the nodes array (nods) length nods.length = 4; // set the position of the 4 nodes in the second contour in a box shape // that lies within the box shape of the first contour also positioned // around the location of the mouse SetNodePosition(nods[0], AddPoints(mouse, {x:-15, y:-15})); SetNodePosition(nods[1], AddPoints(mouse, {x:15, y:-15})); SetNodePosition(nods[2], AddPoints(mouse, {x:15, y:15})); SetNodePosition(nods[3], AddPoints(mouse, {x:-15, y:15})); // create a new control point in the first position of the control points array cps[0] = new ControlPoint(); // set the first control point to the position of the mouse with the name // "Center" and the toolTip "Click Me" SetControlPoint(cps[0], mouse, "Center", "Click Me"); // for BeginDragInsert only: // define a variable to hold a default RegisterMoveParms object var parms = smartShape.GetDefaultMoveParms(); // assign a short variable to represent the first (outer) contour's nodes array nods = smartShape.elem.elements[0].contours[0].nodes; // set delta X and Y ratios so that the first node (top left corner) // doesn't move with the mouse at all applying it with a RegisterInsertBBoxMove parms.deltaXtoX = 0; parms.deltaYtoY = 0; nods[0].RegisterInsertBBoxMove(parms); // set delta X and Y ratios so that the second node (top right corner) // only moves with the mouse x applying it with a RegisterInsertBBoxMove parms.deltaXtoX = 1; parms.deltaYtoY = 0; nods[1].RegisterInsertBBoxMove(parms); // set delta X and Y ratios so that the third node (bottom right corner) // so it moves with the mouse applying it with a RegisterInsertBBoxMove parms.deltaXtoX = 1; parms.deltaYtoY = 1; nods[2].RegisterInsertBBoxMove(parms); // set delta X and Y ratios so that the fourth node (bottom left corner) // only moves with the mouse y applying it with a RegisterInsertBBoxMove parms.deltaXtoX = 0; parms.deltaYtoY = 1; nods[3].RegisterInsertBBoxMove(parms); // set delta X and Y ratios so that the central control point // moves half the distance of the mouse applying it with a RegisterInsertBBoxMove parms.deltaXtoX = 1/2; parms.deltaYtoY = 1/2; cps[0].RegisterInsertBBoxMove(parms); // assign a short variable to represent the second (inner) contour's nodes array nods = smartShape.elem.elements[0].contours[1].nodes; // set delta X and Y ratios so that the first node (top left corner) // doesn't move with the mouse at all except based on 1/5 the distance // that of the shortest side dragged (bringing it inwards by that amount) // applying it with a RegisterInsertBBoxMove parms.deltaXtoX = 0; parms.deltaYtoY = 0; parms.deltaShortestSideToX = 1/5; parms.deltaShortestSideToY = 1/5; nods[0].RegisterInsertBBoxMove(parms); // set delta X and Y ratios so that the second node (top right corner) // only moves with the mouse x normally but removing an additional 1/5 // of the shortest side from the x and setting y to move only 1/5 of // the shortest side applying it with a RegisterInsertBBoxMove parms.deltaXtoX = 1; parms.deltaYtoY = 0; parms.deltaShortestSideToX = -1/5; parms.deltaShortestSideToY = 1/5; nods[1].RegisterInsertBBoxMove(parms); // set delta X and Y ratios so that the third node (bottom right corner) // so it moves with the mouse except subtracting from it 1/5 the distance // that of the shortest side dragged applying it with a RegisterInsertBBoxMove parms.deltaXtoX = 1; parms.deltaYtoY = 1; parms.deltaShortestSideToX = -1/5; parms.deltaShortestSideToY = -1/5; nods[2].RegisterInsertBBoxMove(parms); // set delta X and Y ratios so that the fourth node (bottom left corner) // only moves with the mouse y normally but removing an additional 1/5 // of the shortest side from the y and setting x to move only 1/5 of // the shortest side applying it with a RegisterInsertBBoxMove parms.deltaXtoX = 0; parms.deltaYtoY = 1; parms.deltaShortestSideToX = 1/5; parms.deltaShortestSideToY = -1/5; nods[3].RegisterInsertBBoxMove(parms); } // end event handlers // custom functions: /** * SetBezierNodePosition sets the position of the passed node to the * position of the point pt parameter. All node control points are * set to this point as well * Requires: SetBezierNodePosition */ SetNodePosition = function(node, pt){ SetBezierNodePosition(node, pt,pt,pt); // set point position for all nodes to pt } /** * SetBezierNodePosition sets the position of the passed node to the * position of the points ptp (node predecessor), pt (main point), and * pts (node successor) */ SetBezierNodePosition = function(node, ptp, pt, pts){ node.predX = ptp.x; node.predY = ptp.y; // Predecessor point node.x = pt.x; node.y = pt.y; // Main points node.succX = pts.x; node.succY = pts.y; // Successor points } /** * SetControlPoint positions the passed control point cp to the location * specified by pt and assigns to it name and toolTip */ SetControlPoint = function(cp, pt, name, toolTip){ cp.x = pt.x; // set x position from x of pt cp.y = pt.y; // set y position from y of pt cp.name = name; // set control point name to name passed cp.toolTip = toolTip; // set control point toolTip to toolTip passed } /** * AddPoints adds two points pt1 and pt2 and returns the resulting point */ AddPoints = function(pt1, pt2){ return {x:pt1.x + pt2.x, y:pt1.y + pt2.y}; // add x and y properties in returned object } // end custom functions // invoke event handler: // if the event specified by smartShape.operation exists // in the operation object, call that event as a function if (operation[smartShape.operation]) operation[smartShape.operation]();