Canvas snippets: Difference between revisions

Jump to navigation Jump to search
m
Line 234: Line 234:
| {{Caution|This is currently untested code}}
| {{Caution|This is currently untested code}}
<syntaxhighlight lang="nasal" enclose="div">
<syntaxhighlight lang="nasal" enclose="div">
##
# this is the function that draws a symbol using OpenVG paths
# it accepts a group to draw to and returns the rendered group
# to the caller
var drawVOR = func(group) {
var drawVOR = func(group) {
     return group.createChild("path")
     return group.createChild("path")
Line 243: Line 247:
         .lineTo(-7.5,-12.5)
         .lineTo(-7.5,-12.5)
         .close()
         .close()
         .setStrokeLineWidth(line_width)
         .setStrokeLineWidth(line_width) # style-able
         .setColor(color);
         .setColor(color); # style-able
};
};


var style = { # style to use by default
var style = { # styling related attributes (as per the draw* function above)
     line_width: 3,
     line_width: 3,
    range_line_width: 3,
    radial_line_width: 3,
    range_dash_array: [5, 15, 5, 15, 5],
    radial_dash_array: [15, 5, 15, 5, 15],
     scale_factor: 1,
     scale_factor: 1,
     active_color: [0, 1, 0],
     color: [1,0,0],
    inactive_color: [0, 0.6, 0.85],
};
};


var myCache = StyleableCacheable.new(
# create a new cache entry for the styled symbol
     name:'VOR', draw_func: drawVOR,
var myCachedSymbol = StyleableCacheable.new(
     cache: canvas.SymbolCache32x32,
     name:'myVOR', draw_func: drawVOR,
     cache: canvas.SymbolCache32x32, # the cache to be used
     draw_mode: SymbolCache.DRAW_CENTERED,
     draw_mode: SymbolCache.DRAW_CENTERED,
     relevant_keys: ["line_width", "color"],
     relevant_keys: ['line_width', 'color'], # styling related attributes
);
);
var target = root.createChild('group');
# look up the raster image for the symbol
# render it using the passed style and adjust scaling
var instanced = myCachedSymbol.render(target, style).setScale(0.3);


</syntaxhighlight>
</syntaxhighlight>

Navigation menu