0.4.7 • Published 8 years ago

g2d-mec v0.4.7

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

License npm npm

g2.mec.js

g2 extension for mechanical applications

Mechanical Symbols

g2.mec.js provides some useful mechanical symbols for the g2.js graphics library.

img.symbols.mec

Using these symbols is easily done with the help of the use command. While they are available through the g2.symbol namespace, the bare symbol name is sufficient as the first parameter. We can translate, rotate and scale the symbols as expected. Please note, that their line width is mostly immune against scaling.

    g = g2().use(g2.symbol.origin,{x:100,y:50});
    // or alternatively ...
    g = g2().use("origin",{x:100,y:50});

Mechanical Elements

g2.mec.js also provides some mechanical elements.

img.elements.mec

Element functions have individual arguments.

ElementImageMeaning
vec(p,r,style)vec-defLinear vector element by start point p and end point or direction r.
avec(p,r,w,dw,style)avec-defArc vector element by center point p, radius r, start angle w and angular range dw.
dim(p,r,args)dim-defLinear dimension element by start point p and end point or direction r.
adim(p,r,w,dw,args)adim-defArc dimension element by center point p, radius r, start angle w and angular range dw.
slider(p,w,args)slider-defSlider element by center point p and rotation angle w.
spring(p,r,args)spring-defSymbolical Spring element by start point p and end point or direction r.
damper(p,r,args)damper-defSymbolical Damper element by start point p and end point or direction r.
ground(pts,closed,args)ground-defPolygonial ground element by points array pts and closed flag closed.
bar(p,r)bar-defBar element by start point p and end point or direction r.
bar2(p,r)bar2-defAlternate Bar2 element by start point p and end point or direction r.
link(pts,closed)link-defLink element by points array pts and closed flag true,false.
link2(pts,closed)link2-defAlternate Link2 element by points array pts and closed flag true,false.
beam(pts)beam-defBeam element by points array pts.
beam2(pts)beam2-defAlternate Beam2 element by points array pts.
load(pts,spacing,style)load-defLoad element by points array pts and spacing spacing between arrows.
pulley(p,r)pulley-defPulley element by center point p and radius r.
pulley2(p,r)pulley2-defPulley2 element by position pos={x,y,w} including rotation angle w and radius r.
rope(p1,r1,p2,r2)rope-defRope element tangential to two circles with given center points p1 and p2 and radii r1 and r2. You can switch between the four possible tangents by the sign of the radii.

Mechanical Styles

There are some predefined colors, line styles and other constants. You can overwrite them, if you are not comfortable with it.

NameTypeDefaultDescription
Stateobjectg2 state namespace.
State.nodcolorstring"#333"node color.
State.nodfillstring"#dedede"node fill color.
State.nodfill2string"#aeaeae"alternate node fill color, somewhat darker.
State.linkcolorstring"#666"link color.
State.linkfillstring"rgba(200,200,200,0.5)"link fill color, semi-transparent.
State.solidarray[]solid line style.
State.dasharray15,10dashed line style.
State.dotarray4,4dotted line style.
State.dashdotarray25,6.5,2,6.5dashdotted line style.
State.labelOffsetnumber5default label offset distance.
State.labelSignificantDigitsnumber3default label's significant digits after floating point.

Truss Example

You can see a simple truss below, composed from mechanical symbols and elements.

img.truss.mec

// g2.mec truss example ...
    var A = {x:50,y:50},  B = {x:250,y:50},
        C = {x:50,y:150}, D = {x:150,y:150},
        E = {x:50,y:250},
        g = g2()
             .cartesian()
             .link2([A,B,E,A,D,C])
             .vec(B,{dx:0,dy:-50},{lw:2,ls:"darkred"})
             .vec(D,{dx:80,dy:0},{lw:2,ls:"darkred"})
             .use("nodfix",A)
             .use("nod",B)
             .use("nod",C)
             .use("nod",D)
             .use("nodflt",{x:E.x,y:E.y,w:-Math.PI/2})
             .exe(document.getElementById("c").getContext("2d"));

Labels

If you want to add text to a geometric element, you can always use the basic txt element. However some element types support the smarter label element, which comes with a more intuitive relative positioning with respect to its nearest previous element. Reliable positioning requires always cartesian coordinates.

ElementMeaning
label(str,loc,off)Label element placing string str at location loc using offset off.

Point like and rectangular elements provide locations according cardinal directions. Linear elements provide parameterized numerical or named locations.

TypeElementsdefaultlocationsoffsetimg
Point elementscir, usecce,ne,n,nw,w,sw,s,seangle in radiansnumberimg.point-label.mec
Rectangular elementsrec, slidercce,ne,n,nw,w,sw,s,senumberimg.rec-label.mec
Linear elementslin, vec, dimarc, avec, adimspring, damper bar, bar20.5beg, mid, end normalized numerical parameterleft, rightnumberline with labels
Polygonial elementsply, groundlink, link20.5beg, end #index normalized numerical parameterleft, rightnumberpolygon with labels
Spline elementsplinebegbeg, end #indexleft, rightnumberspline with labels

A numerical offset always means outside of closed shapes with regard to the specified point. With linear, polygonial and spline elements a positive value means right of and a negativ value means left of the line at the specified point. If there is no offset distance specified, the global g2.State.labelOffset's value is taken. Please note, that cardinal locations are not sensitive to transformations.

Truss Example with Labels

The truss example above can now be improved by adding labels.

truss with labels

  // g2.mec truss example with labels ...
     var A = {x:50,y:50},  B = {x:250,y:50},
         C = {x:50,y:150}, D = {x:150,y:150},
         E = {x:50,y:250},
         g = g2()
              .cartesian()
              .style({foz:12,fof:"cursive",foc:"green"})
              .bar2(A,B).label("1")
              .bar2(A,C).label("2")
              .bar2(A,D).label("3")
              .bar2(B,D).label("4")
              .bar2(C,D).label("5")
              .bar2(C,E).label("6")
              .bar2(D,E).label("7")
              .style({foc:"@nodcolor"})
              .vec(B,{dx:0,dy:-50},{lw:2,ls:"brown"}).label("F","end","left")
              .vec(D,{dx:80,dy:0},{lw:2,ls:"brown"}).label("2F","end")
              .use("nodfix",A).label("A","w")
              .use("nod",B).label("B","se")
              .use("nod",C).label("C","w")
              .use("nod",D).label("D","ne")
              .use("nodflt",{x:E.x,y:E.y,w:-Math.PI/2}).label("E","e")
              .style({ls:"@dimcolor",foc:"@ls"})
              .dim({x:A.x,y:275},{dx:100,dy:0}).label("b")
              .dim({x:D.x,y:275},{dx:100,dy:0}).label("b")
              .dim({x:275,y:E.y},{dx:0,dy:-100}).label("b")
              .dim({x:275,y:D.y},{dx:0,dy:-100}).label("b")
              .exe(document.getElementById("c").getContext("2d"));

Markers

Markers can be placed onto the outline of the nearest previous element.

ElementMeaning
mark(type,loc,dir)Marker element placing marker symbol type at locations loc regarding to direction dir.

Elements with a unique center and rectangular elements provide locations according cardinal directions. Linear elements provide parameterized numerical or named locations. The spline element only support indexed locations. It does not support parameterized locations.

TypeElementsdefaultlocationsdirimg
Centered elementscircce,ne,n,nw,w,sw,s,senormalized parameter-1,0,1circular markers
Rectangular elementsrec, slidercce,ne,n,nw,w,sw,s,se-1,0,1rectangular markers
Linear elementslin, vec, dimarc, avec, adimspring, damper bar, bar20.5beg, mid, end normalized numerical parameter-1,0,1linear markers
Polygonial elementsply, groundlink, link20.5beg, end, mid, all #index normalized numerical parameter-1,0,1polygonial markers
Spline elementsplinebegbeg, end, mid, all #index-1,0,1spline markers

API Reference

See the API Reference for details.

Tests

See this growing table of test cases with canvas and svg output side by side.

GitCDN

Use the link https://gitcdn.xyz/repo/goessner/g2-mec/master/g2.mec.min.js for getting the latest commit as a raw file.

In HTML use ...

<script src="https://gitcdn.xyz/repo/goessner/g2-mec/master/g2.mec.min.js"></script>

License

g2.mec is licensed under the terms of the MIT License.

#Change Log

0.4.7 - 2016-09-09

Modified

  • label offset bug removed.

0.4.5 - 2016-08-01

Modified

  • Use of mark and label element requires cartesian flag set from now on.
  • use command execution simplified.
  • styling bug with g2.prototype.use removed.

0.4.4 - 2016-06-21

Added

  • g2.spline performing 'centripetal Catmull-Rom' interpolation.

Modified

  • modified g2.prototype.ply.iterator integrated.

0.4.0 - 2016-05-24

Added

link, bar, pulley, rope elements added.

0.3.0 - 2016-05-13

Changed

  • Fundamental changes also affecting the api.
  • See documentation and API for details. @goessner.

0.2.3 - 2016-05-03

Changed

Transformation bug removed @goessner.

0.2.0 - 2016-01-10

Added

CHANGELOG.md @goessner.