forked from NorthwoodsSoftware/GoJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakingImages.html
300 lines (263 loc) · 9.52 KB
/
makingImages.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>GoJS Making Images -- Northwoods Software</title>
<!-- Copyright 1998-2019 by Northwoods Software Corporation. -->
<script src="../release/go.js"></script>
<script src="goIntro.js"></script>
<style type="text/css">
.images {
border: 1px solid rgba(255,0,0,.4);
}
/* make HRs thicker to set them apart from the code section borders */
hr {
height: 3px;
background: #333;
}
</style>
</head>
<body onload="goIntro()">
<div id="container" class="container-fluid">
<div id="content">
<h1>Making Images</h1>
<p>
<b>GoJS</b> has two functions for creating images: <a>Diagram.makeImageData</a>, which outputs a Base64 image data string, and <a>Diagram.makeImage</a>, which is a convenience function that calls <a>Diagram.makeImageData</a> and returns a new HTMLImageElement with the image data as its source. Both functions have the same single argument, a JavaScript Object that contains several definable properties, enumerated in the documentation.
</p>
<p>
This page is almost identical to the page on <a href="makingSVG.html">Making SVG</a>, which shows how to render SVG elements instead of PNG images.
</p>
<!-- Don't bother showing this source -->
<pre class="lang-js" id="diag" style="display: none;">
// define a simple Node template (but use the default Link template)
diagram.nodeTemplate =
$(go.Node, "Auto",
$(go.Shape, "RoundedRectangle",
// Shape.fill is bound to Node.data.color
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 3 }, // some room around the text
// TextBlock.text is bound to Node.data.key
new go.Binding("text", "key"))
);
// create the model data that will be represented by Nodes and Links
var nodeDataArray = [
{ key: "Alpha", color: "lightblue" },
{ key: "Beta", color: "orange" },
{ key: "Gamma", color: "lightgreen" },
{ key: "Delta", color: "pink" }
];
var linkDataArray = [
{ from: "Alpha", to: "Beta" },
{ from: "Alpha", to: "Gamma" },
{ from: "Beta", to: "Beta" },
{ from: "Gamma", to: "Delta" },
{ from: "Delta", to: "Alpha" }
];
diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
window.myDiagram = diagram;
window.goCode2 = function(pre) {
if (diagramclass === undefined) diagramclass = go.Diagram;
if (typeof pre === "string") pre = document.getElementById(pre);
var f = eval("(function () {window.img = " + pre.textContent + "})");
f();
img.className = "images";
pre.parentElement.insertBefore(img, pre.nextSibling)
}
window.goCode3 = function(pre) {
if (diagramclass === undefined) diagramclass = go.Diagram;
if (typeof pre === "string") pre = document.getElementById(pre);
var f = eval("(function () {" + pre.textContent + "})");
f();
}
window.addImage = function(img) {
obj = document.getElementById("severalImages");
img.className = "images";
obj.appendChild(img)
}
</pre>
<script>goCode("diag", 300, 150)</script>
<hr/>
<p>
Calling makeImage with no arguments or with an empty properties object results in an image that is the same size as the Diagram's viewport.
</p>
<pre class="lang-js" id="code0">
myDiagram.makeImage();
</pre>
<script>goCode2("code0");</script>
<hr/>
<p>
Calling makeImage with an object that has the "scale" property set to 1 results in an image that includes the whole diagram,
not just the area visible in the viewport. However, the empty areas around the document bounds are trimmed away.
</p>
<pre class="lang-js" id="codeA">
myDiagram.makeImage({
scale: 1
});
</pre>
<script>goCode2("codeA");</script>
<hr/>
<p>
Setting the scale property will create a scaled image that is precisely large enough to contain the Diagram. The following image is created with a scale of 2.
</p>
<pre class="lang-js" id="code1">
myDiagram.makeImage({
scale: 2
});
</pre>
<script>goCode2("code1");</script>
<hr/>
<p>
The following image is created by setting the size option of makeImage. Note that the canvas is scaled uniformly and any extra space is placed on the bottom or right side of the image.
</p>
<pre class="lang-js" id="code2">
myDiagram.makeImage({
size: new go.Size(100,100)
});
</pre>
<script>goCode2("code2");</script>
<hr/>
<p>The following image is also created by setting the size option of makeImage, but only the width is set. The height will be whatever size is needed to uniformly contain the Diagram.</p>
<pre class="lang-js" id="code3">
myDiagram.makeImage({
size: new go.Size(100,NaN)
});
</pre>
<script>goCode2("code3");</script>
<hr/>
<p>The parts option allows us to specify an <a>Iterable</a> collection of Parts to draw. This is useful if you only want to make an image of part of the diagram, such as a selection of nodes.</p>
<pre class="lang-js" id="code4a">
myPartsList = new go.List();
myPartsList.add(myDiagram.findNodeForKey("Beta"));
myPartsList.add(myDiagram.findNodeForKey("Delta"));
</pre>
<script>goCode3("code4a");</script>
<pre class="lang-js" id="code4">
myDiagram.makeImage({
parts: myPartsList
});
</pre>
<script>goCode2("code4");</script>
<p>Or simply drawing only the links:</p>
<pre class="lang-js" id="code4-2">
myDiagram.makeImage({
parts: myDiagram.links
});
</pre>
<script>goCode2("code4-2");</script>
<hr/>
<p>Setting both scale and size creates an image that is scaled specifically and cropped to the given size, as in the following image.</p>
<pre class="lang-js" id="code5">
myDiagram.makeImage({
scale: 1.5,
size: new go.Size(100,100)
});
</pre>
<script>goCode2("code5");</script>
<hr/>
<p>We may want a very large, scaled image that has a limit on its size, and we can use the maxSize property to constrain one or both dimensions. The following image has a very large scale applied but is limited in size horizontally, so some horizontal cropping will occur.</p>
<p>The default value for maxSize is <code>go.Size(2000, 2000)</code>, and specifying <code>go.Size(600, NaN)</code> is equivalent to specifying <code>go.Size(600, 2000)</code>. If we wanted no cropping on the height we could instead write <code>go.Size(600, Infinity)</code>.</p>
<pre class="lang-js" id="code6">
myDiagram.makeImage({
scale: 9,
maxSize: new go.Size(600, NaN)
});
</pre>
<script>goCode2("code6");</script>
<hr/>
<p>Setting both position and size creates a diagram image that is positioned specifically and cropped to the given size. When a position is set but no scale is set, the scale defaults to 1.</p>
<pre class="lang-js" id="code7">
myDiagram.makeImage({
position: new go.Point(20,20),
size: new go.Size(50,50)
});
</pre>
<script>goCode2("code7");</script>
<p>Setting the background to a CSS color string will replace the transparent Diagram background with the given color.</p>
<pre class="lang-js" id="code8">
myDiagram.makeImage({
size: new go.Size(NaN,250),
background: "rgba(0, 255, 0, 0.5)" // semi-transparent green background
});
</pre>
<script>goCode2("code8");</script>
<hr/>
<p>In the following code we use the document bounds to split the Diagram into four equal parts, making four images out of each part. In this way we can prepare images for pagination, making a gallery, or printing purposes. The four images created are shown below.</p>
<pre class="lang-js" id="manyImgCode">
var d = myDiagram.documentBounds;
var halfWidth = d.width / 2;
var halfHeight = d.height / 2;
img = myDiagram.makeImage({
position: new go.Point(d.x, d.y),
size: new go.Size(halfWidth,halfHeight)
});
addImage(img); // Adds the image to a DIV below
img = myDiagram.makeImage({
position: new go.Point(d.x + halfWidth, d.y),
size: new go.Size(halfWidth,halfHeight)
});
addImage(img);
img = myDiagram.makeImage({
position: new go.Point(d.x, d.y+ halfHeight),
size: new go.Size(halfWidth,halfHeight)
});
addImage(img);
img = myDiagram.makeImage({
position: new go.Point(d.x + halfWidth, d.y + halfHeight),
size: new go.Size(halfWidth,halfHeight)
});
addImage(img);
</pre>
<div id="severalImages"></div>
<script>goCode3("manyImgCode");</script>
<hr/>
<h2 id="ImageType">Image Type</h2>
<p>
We can set the type and details properties of the argument object in order to retrieve different kinds of images. The only widely supported type is "image/jpeg". The details for a jpeg determine its quality by using values from 0 to 1 inclusive. Jpegs are not commonly used for Diagrams because their lossy compression can render text unreadable.
</p>
<p>
The following image is an outputted jpeg. Note how the transparent background is turned black, because the jpeg format does not support alpha transparency, and the default state of the HTML5 canvas is that of fully transparent black pixels, rgba(0,0,0,0).
</p>
<pre class="lang-js" id="codea1">
myDiagram.makeImage({
scale: 1,
type: "image/jpeg"
});
</pre>
<script>
goCode2("codea1");
</script>
<hr/>
<p>
The following image is a jpeg created with an AntiqueWhite background specified.
</p>
<pre class="lang-js" id="codea2">
myDiagram.makeImage({
scale: 1,
background: "AntiqueWhite",
type: "image/jpeg"
});
</pre>
<script>
goCode2("codea2");
</script>
<hr/>
<p>
The following image is a jpeg created (with an AntiqueWhite background) and the details option, at very low quality.
</p>
<pre class="lang-js" id="codea3">
myDiagram.makeImage({
scale: 1,
background: "AntiqueWhite",
type: "image/jpeg",
details: 0.05
});
</pre>
<script>
goCode2("codea3");
</script>
</div>
</div>
</body>
</html>