In touch with geometry!

Info

Embed sketchometry

Starting with version 1.2.0 it is possible to embed sketchometry in web pages like learning management systems or content management systems. The preferred way to do this is to use an iframe and control sketchometry from the hosting web site with postMessage.

Send messages to sketchometry

Let’s suppose that sketchometry is embedded in the web page in an iframe with id sketchometry. Then, the hosting web page is communicating with the sub-object contentWindow of the iframe’s DOM node:

1 var skm = document.getElementById("sketchometry").contentWindow;
2     skm.postMessage({'code': 'init'}, URL);

the variable URL contains the URL where sketchometry is loaded from. Here, one can use either https://embed.sketchometry.org or download the zip file containing sketchometry and put it on a dedicated web server.

Receive messages from sketchometry

The only situation where sketchometry sends a message back to the hosting web page is after a save message. Before the hosting web site can receive postMessages from an iframe an eventhandler for the event message has to be customized. Examples:

 1 var receiveMessage = function(evt) {
 2     var content;
 3 
 4     if (evt.origin !== "https://embed.sketchometry.org") {
 5         console.log("REJECT", evt.origin)
 6         return;
 7     }
 8 
 9     if (evt.data.code === 'save') {
10         content = evt.data.data;
11 
12         // Write construction contained in content to database
13         console.log("RECEIVED", content);
14     }
15 };
16 
17 window.addEventListener("message", receiveMessage, false);

But first, the saving of a construction has to be triggered by a save message:

1 skm.postMessage({'code': 'save'}, URL);

Possible messages

  • {'code': 'init'}: Prepare embedded version of sketchometry
  • {'code': 'load', 'data': construction string}: Load base64 encoded sketch string
  • {'code': 'new', 'name': 'construction name'}: Open new construction
  • {'code': 'save'}: get the actual construction as base64 encoded sketch string. sketchometry will send a postMessage to the hosting web page of the form {‘code’: ‘save’, ‘data’: content}
  • {'code': 'set', 'data': {settingId': 'key', 'value': val}}: Change settings, see settings.js.
    • Examples are: {'code': 'set', 'data': {'settingId': 'language', 'value': 'de'}
  • {'code': 'clear'}: Removes all constructions from local storage.

Example

Here is an example of a hosting web page which enables the control of sketchometry by buttons:

 1 <html>
 2 <head><title>embed sketchometry</title></head>
 3 <script type="text/javascript">
 4 
 5 var URL = 'https://embed.sketchometry.org';
 6 
 7 var init = function() {
 8     var skm = document.getElementById("sketchometry").contentWindow;
 9     skm.postMessage({'code': 'init'}, URL);
10 };
11 
12 var showCircle = function() {
13     var construction = "UEsDBAoAAAAIAKKSs ... AAALQIAAAAA";
14 
15     var skm = document.getElementById("sketchometry").contentWindow;
16     skm.postMessage({'code': 'load',
17                      'data': construction},
18         URL);
19 };
20 
21 var newConstruction = function() {
22     var skm = document.getElementById("sketchometry").contentWindow;
23     skm.postMessage({'code': 'new', 'name': 'new file'}, URL);
24 };
25 
26 var save = function() {
27     var skm = document.getElementById("sketchometry").contentWindow;
28     skm.postMessage({'code': 'save'}, URL);
29 };
30 
31 var setLang = function(lang) {
32     var skm = document.getElementById("sketchometry").contentWindow;
33     skm.postMessage({'code': 'set',
34                      'data': {'settingId': 'language', 'value': lang}
35                     }, URL);
36 };
37 
38 var clearAll = function() {
39     var skm = document.getElementById("sketchometry").contentWindow;
40     skm.postMessage({'code': 'clear'}, URL);
41 };
42 
43 var receiveMessage = function(evt) {
44     var content;
45 
46     if (evt.origin !== "https://embed.sketchometry.org") {
47         console.log("REJECT", evt.origin)
48         return;
49     }
50 
51     if (evt.data.code === 'save') {
52         content = evt.data.data;
53 
54         // Write construction contained in content to database
55         console.log("RECEIVED", content);
56     }
57 };
58 
59 window.addEventListener("message", receiveMessage, false);
60 
61 </script>
62 <body>
63 <h1>This is a web page containing sketchometry</h1>
64 <iframe id="sketchometry" width="800" height="600"
65     src="https://embed.sketchometry.org/index.html"
66     onLoad="init();"></iframe>
67 <p>
68 <button onClick="showCircle();">Load construction</button>
69 <button onClick="newConstruction();">New construction</button>
70 <button onClick="save();">Save construction</button>
71 <button onClick="setLang('de');">Choose language "Deutsch"</button>
72 <button onClick="setLang('en');">Choose language "English"</button>
73 <button onClick="clearAll();">Clear gallery</button>
74 </p>
75 </body>
76 </html>

The live demo of this example is at https://embed.sketchometry.org/iframe.html.

Enjoy, Alfred