翻译|使用教程|编辑:吴园园|2019-09-04 16:28:24.653|阅读 615 次
概述:保持色彩信息,并说明如何绘制形状的内侧或形状的笔划或一个TextBlock或任何GraphObject的背景。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
GoJS是一款功能强大,快速且轻量级的流程图控件,可帮助你在JavaScript 和HTML5 Canvas程序中创建流程图。
Solid Brushes
最简单的画笔由单一纯色定义。因为它们非常简单,只要你想要一个单色画笔,你就可以替换一个有效的CSS颜色字符串。
diagram.add($(go.Part,
$(go.Shape, "Circle", { fill: $(go.Brush, { color: "palegreen" })
})
));
diagram.add($(go.Part,
$(go.Shape, "Circle", { fill: "palegreen"
})
));
许多CSS颜色字符串都是有效的,包括命名颜色,十六进制值,RGB值和RGBA值。
diagram.layout = $(go.GridLayout);
diagram.add($(go.Part,
$(go.Shape, "Circle", { fill: "#DFAD83"
})
));
diagram.add($(go.Part,
$(go.Shape, "Circle", { fill: "rgba(0,255,0,.3)" // semi transparent green
})
));
diagram.add($(go.Part,
$(go.Shape, "Circle", { fill: "rgba(0,255,0,.3)", stroke: '#DFBB00', strokeWidth: 4, background: 'coral'
})
));
渐变画笔
通过设置类型并向Brush添加一些色标来定义渐变画笔。
// constructs a Linear gradient brush
var brush = new go.Brush(go.Brush.Linear);
brush.addColorStop(0, "blue");
brush.addColorStop(1, "red");
要简化语法,可以使用go.GraphObject.make
// constructs the same Brush
var brush = $(go.Brush, "Linear", { 0.0: "blue", 1.0: "red" });
一些例子如下:
diagram.add(
$(go.Part, "Table",
$(go.Shape, { row: 0, column: 0, figure: "Circle", width: 100, height: 100, margin: 5, // A linear gradient brush from blue to red, going from top to bottom (default)
fill: $(go.Brush, "Linear", { 0.0: "blue", 1.0: "red" })
}),
$(go.Shape, { row: 0, column: 1, figure: "Circle", width: 100, height: 100, margin: 5, // A linear gradient brush from blue to red, going from bottom to top
// by defining start and end spots
fill: $(go.Brush, "Linear", { 0.0: "blue", 1.0: "red", start: go.Spot.Bottom, end: go.Spot.Top })
})
));
Brush可以有任意数量的颜色停止:
diagram.add(
$(go.Part, "Table",
$(go.Shape, { row: 0, column: 0,
figure: "Rectangle", width: 100, height: 100, margin: 5,
// A rainbow linear gradient brush:
fill: $(go.Brush, "Linear", {
0.0: "rgba(255, 0, 0, 1)",
0.15: "rgba(255, 255, 0, 1)",
0.30: "rgba(0, 255, 0, 1)",
0.50: "rgba(0, 255, 255, 1)",
0.65: "rgba(0, 0, 255, 1)",
0.80: "rgba(255, 0, 255, 1)",
1: "rgba(255, 0, 0, 1)"
})
}),
$(go.Shape, { row: 0, column: 1,
figure: "Rectangle", width: 100, height: 100, margin: 5,
// A rainbow radial gradient brush:
fill: $(go.Brush, "Radial", {
0.0: "rgba(255, 0, 0, 1)",
0.15: "rgba(255, 255, 0, 1)",
0.30: "rgba(0, 255, 0, 1)",
0.50: "rgba(0, 255, 255, 1)",
0.65: "rgba(0, 0, 255, 1)",
0.80: "rgba(255, 0, 255, 1)",
1: "rgba(255, 0, 0, 1)"
})
})
));
可以使用Brsuh.startRadius和Brush.endRadius控制径向渐变画笔,它们分别默认为零和NaN,这意味着渐变从正中心开始并且到达对象的最远测量边缘。
diagram.layout = $(go.GridLayout);
diagram.add(
$(go.Part,
$(go.Shape, {
figure: "Rectangle", width: 100, height: 100, margin: 5,
// A rainbow radial gradient brush:
fill: $(go.Brush, "Radial", {
0.0: "red", 1: "black"
})
})
));
diagram.add(
$(go.Part,
$(go.Shape, {
figure: "Rectangle", width: 100, height: 100, margin: 5,
// A rainbow radial gradient brush:
fill: $(go.Brush, "Radial", {
startRadius: 30, 0.0: "red", 1: "black"
})
})
));
diagram.add(
$(go.Part,
$(go.Shape, {
figure: "Rectangle", width: 100, height: 100, margin: 5,
// A rainbow radial gradient brush:
fill: $(go.Brush, "Radial", {
startRadius: 30, endRadius: 40, 0.0: "red", 1: "black"
})
})
));
几个GraphObjects可以共享相同的Brush:
diagram.layout = $(go.GridLayout); // Create one brush for several GraphObjects to share:
var rainbow = $(go.Brush, "Linear", {
0.0: "rgba(255, 0, 0, 1)",
0.15: "rgba(255, 255, 0, 1)",
0.30: "rgba(0, 255, 0, 1)",
0.50: "rgba(0, 255, 255, 1)",
0.65: "rgba(0, 0, 255, 1)",
0.80: "rgba(255, 0, 255, 1)",
1: "rgba(255, 0, 0, 1)"
});
diagram.add(
$(go.Part,
$(go.Shape, { figure: "Rectangle", width: 100, height: 100, fill: rainbow })
));
diagram.add(
$(go.Part,
$(go.Shape, { figure: "Fragile", width: 50, height: 50, angle: 45, fill: rainbow })
));
diagram.add(
$(go.Part, "Auto",
$(go.Shape, { figure: "Rectangle", fill: rainbow }),
$(go.TextBlock, "text", { font: 'bold 32pt sans-serif', stroke: rainbow, angle: 90 })
));
diagram.add(
$(go.Part,
$(go.Shape, { figure: "Circle", width: 70, height: 70, angle: 180, fill: null, strokeWidth: 10, stroke: rainbow })
));
图案画笔
下面的示例设置了两个Pattern画笔,一个使用HTML Canvas并绘制了内容,如下所示:
另一个Pattern Brush使用此图像:
// set up an 40x40 HTML Canvas and draw on it to create a repeating "tile" to use as a pattern
function makePattern() { var patternCanvas = document.createElement('canvas');
patternCanvas.width = 40;
patternCanvas.height = 40; var pctx = patternCanvas.getContext('2d'); // This creates a shape similar to a diamond leaf
pctx.beginPath();
pctx.moveTo(0.0, 40.0);
pctx.lineTo(26.9, 36.0);
pctx.bezierCurveTo(31.7, 36.0, 36.0, 32.1, 36.0, 27.3);
pctx.lineTo(40.0, 0.0);
pctx.lineTo(11.8, 3.0);
pctx.bezierCurveTo(7.0, 3.0, 3.0, 6.9, 3.0, 11.7);
pctx.lineTo(0.0, 40.0);
pctx.closePath();
pctx.fillStyle = "rgb(188, 222, 178)";
pctx.fill();
pctx.lineWidth = 0.8;
pctx.strokeStyle = "rgb(0, 156, 86)";
pctx.lineJoin = "miter";
pctx.miterLimit = 4.0;
pctx.stroke(); return patternCanvas;
} if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this
var $ = go.GraphObject.make; // for conciseness in defining templates
diagram.nodeTemplate =
$(go.Node, "Spot",
{ resizable: true, resizeObjectName: 'SHAPE' },
$(go.Shape, "Rectangle",
{ name: 'SHAPE', strokeWidth: 0, stroke: null }, new go.Binding("fill")),
$(go.TextBlock,
{ margin: 10, font: "bold 18px Verdana" }, new go.Binding("text", "key"))
); var img = new Image();
img.src = 'images/pattern.jpg'; // Use an image as a pattern
var patternBrush = $(go.Brush, "Pattern", { pattern: img }); // use a reference to an HTML Canvas (with renderings on it) as a pattern:
var patternBrush2 = $(go.Brush, "Pattern", { pattern: makePattern() });
diagram.model = new go.GraphLinksModel(
[
{ key: "Alpha", fill: patternBrush },
{ key: "Beta", fill: patternBrush2 }
],
[
]);
结果:
画笔功能
有一些功能可用于生成不同的颜色或修改画笔颜色:
Brush.randomColor - 返回随机的十六进制颜色值
Brush.lightenBy和Brush.darkenBy - 返回变亮或变暗的颜色/画笔; 有实例和静态版本
Brush.lighten和Brush.darken - 方便的静态函数,可以返回变亮或变暗的颜色
Brush.mix - 将两种颜色混合在一起
Brush.isDark - 确定颜色是否暗,通常用于绑定; 有实例和静态版本
在以下示例中,零件使用“变亮”和“变暗”功能为其笔划/填充获取合适的颜色。
diagram.layout = $(go.GridLayout); var color1 = "rgb(80, 130, 210)"; var color2 = go.Brush.randomColor(192, 224); var gradBrush = $(go.Brush, "Linear", { 0: color1, 1: color2 }); function shapeStyle() { return [ "Ellipse", { width: 120, height: 80, strokeWidth: 4 } ];
} // static Brush methods
diagram.add($(go.Part, "Auto",
$(go.Shape, shapeStyle(),
{ fill: color1, stroke: go.Brush.darken(color1) }),
$(go.TextBlock, "dark stroke")));
diagram.add($(go.Part, "Auto",
$(go.Shape, shapeStyle(),
{ fill: color1, stroke: go.Brush.darkenBy(color1, .4) }),
$(go.TextBlock, "darker stroke")));
diagram.add($(go.Part, "Auto",
$(go.Shape, shapeStyle(),
{ fill: go.Brush.lighten(color1), stroke: color1 }),
$(go.TextBlock, "light fill")));
diagram.add($(go.Part, "Auto",
$(go.Shape, shapeStyle(),
{ fill: go.Brush.lightenBy(color1, .4), stroke: color1 }),
$(go.TextBlock, "lighter fill"))); // instance Brush methods
diagram.add($(go.Part, "Auto",
$(go.Shape, shapeStyle(),
{ fill: gradBrush.copy().lightenBy(.2) }),
$(go.TextBlock, "lighter")));
diagram.add($(go.Part, "Auto",
$(go.Shape, shapeStyle(),
{ fill: gradBrush }),
$(go.TextBlock, "normal")));
diagram.add($(go.Part, "Auto",
$(go.Shape, shapeStyle(),
{ fill: gradBrush.copy().darkenBy(.2) }),
$(go.TextBlock, "darker")));
在以下示例中,文本的颜色取决于背景形状是否为暗。
diagram.layout = $(go.GridLayout);
diagram.nodeTemplate =
$(go.Node, "Auto",
{ desiredSize: new go.Size(80, 40) },
$(go.Shape, "RoundedRectangle", { strokeWidth: 0 }, new go.Binding("fill", "color")),
$(go.TextBlock, { margin: 8 }, new go.Binding("stroke", "color", // dark nodes use white text, light nodes use dark text
function (c) { return go.Brush.isDark(c) ? "white" : "black"; }), new go.Binding("text", "key")
)
);
diagram.model = new go.Model(
[
{ key: "Alpha", color: "white" },
{ key: "Beta", color: "black" },
{ key: "Gamma", color: "darkblue" },
{ key: "Delta", color: "lightblue" },
{ key: "Epsilon", color: "darkgreen" },
{ key: "Zeta", color: "lightgreen" },
{ key: "Eta", color: "darkred" },
{ key: "Theta", color: "lightcoral" }
]
)
=====================================================
想要购买GoJS正版授权的朋友可以。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn
文章转载自: