Difference between revisions of "Reference:GeoGebra Apps Embedding"

From GeoGebra Manual
Jump to: navigation, search
(removed applet type)
(For technical reasons, you need to use <code>5.2</code> not <code>5.0</code> after version 804)
(42 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{Reference}}
+
This page describes how to embed GeoGebra Apps into your website. For more information on how to interact with embedded apps, please see [[Reference:GeoGebra_Apps_API|GeoGebra Apps API]].  
This article describes how to embed GeoGebra Math Apps into your website or offline HTML materials using Javascript.
 
{{Note|Alternative solution is to embed materials from GeoGebra Materials Platform in your website as an iframe. See [[Reference:Material_Embedding_(Iframe)]] for details.}}
 
=Location of Math Apps=
 
You can use Math Apps from our CDN (app.geogebra.org) or you can download them as ''Math Apps Bundle'' that you can host on your own server or use offline.
 
  
[https://www.geogebra.org/partner/portal Download Math Apps Bundle]
+
=Quick Start=
=Embed using Javascript=
+
In order to embed GeoGebra apps into your page, you need to add the following three parts:
This method is recommended when multiple HTML5 applets are embedded in one page, or if you need to use the [https://www.geogebra.org/manual/en/Reference:JavaScript JavaScript API]. In this case they can share one instance of the GeoGebra codebase and load faster.
 
Here is an example web page that shows how to embed applets: [http://dev.geogebra.org/examples/html/example2.html Example page].
 
  
You need to include the following three parts in your HTML file:
+
{{Step|num=1}} Make sure you have this in the <code>&lt;head></code> section to make sure scaling works correctly (eg on mobile browsers) and that Unicode works
 +
<pre><meta name=viewport content="width=device-width,initial-scale=1"> 
 +
<meta charset="utf-8"/></pre>
  
{{Step|num=1}} The javascript library deployggb.js needs to be included with this tag:
+
{{Step|num=2}} The javascript library deployggb.js needs to be included with this tag:
 +
<pre><script src="https://www.geogebra.org/apps/deployggb.js"></script></pre>
  
<pre>   <script src="https://cdn.geogebra.org/apps/deployggb.js"></script></pre>
+
{{Step|num=3}} Create an element for the GeoGebra app on your page:
{{Step|num=2}} For each applet a javaScript object of the type GGBApplet needs to be created. It takes at least the material ID or URL as a parameter.
+
<pre><div id="ggb-element"></div> </pre>
  
Here is the according code from the [http://dev.geogebra.org/examples/html/example2.html example page] that creates 2 applets:
+
{{Step|num=4}} Configure and insert the app:
<pre>   var applet1 = new GGBApplet({material_id: "17499", borderColor:"#55FF00"}, true);
+
<pre><script> 
     var applet2 = new GGBApplet({filename: "../ggb/sine-curves.ggb","showtoolbar":true}, true);
+
    var params = {"appName": "graphing", "width": 800, "height": 600, "showToolBar": true, "showAlgebraInput": true, "showMenuBar": true };
  window.addEventListener("load", function() {
+
     var applet = new GGBApplet(params, true);
         applet1.inject('applet_container1');
+
    window.addEventListener("load", function() {  
        applet2.inject('applet_container2');
+
         applet.inject('ggb-element');
     });</pre>
+
     });
 +
</script></pre>
  
The above example shows how to load a file using the filename or the id of the material on GeoGebra Materials Platform. For offline use you will need to specify the file using a base64 encoded string (press Ctrl+Shift+B to export ggbBase64 string to clipboard).  
+
Simply change the <code>appName</code> parameter from <code>graphing</code> to <code>geometry</code> or <code>3d</code> to get one of our other apps. To load an activity you can use eg <code>"material_id":"RHYH3UQ8"</code> or <code>"filename":"myFile.ggb"</code> and you can customize our apps further by using various [[Reference:GeoGebra_App_Parameters|GeoGebra App Parameters]].
  
When used with the id parameter, the applet will be created using the dimensions (width and height) and the applet parameters defined in the material settings on GeoGebra Materials Platform.  Optionally you can override these settings by passing them in the parameters. See [[Reference:Applet_Parameters]] for a list of applet parameters.
+
=Tutorial=
  
In the example the parameter 'showMenuBar:true' overrides the material setting from tube to show the menu bar for applet3.
+
Please see here for a longer tutorial which explains the different embedding options:
 +
https://www.geogebra.org/m/sehv2qc9
  
The parameter of the method <code>inject()</code> is a string with the id of the HTML object that will be the parent of the applet.  
+
=Live Examples=
 +
The following examples and their html source code show how to use GeoGebra apps either embedded in your page or in popup dialogs:
 +
* [http://dev.geogebra.org/examples/html/example-graphing.html Graphing Calculator embedded]
 +
* [http://dev.geogebra.org/examples/html/example-geometry.html Geometry app embedded]
 +
* [http://dev.geogebra.org/examples/html/example-popup-graphing.html Graphing Calculator popup]
 +
* [http://dev.geogebra.org/examples/html/example-popup-geometry.html Geometry app popup]
  
{{Step|num=3}} Finally, you need to determine where the applet should appear in the document and inject the applet. In our example file we create a div as parent for each applet, like this:
+
=GeoGebra Apps API=
 +
The following examples show how you can interact with embedded apps through the [https://wiki.geogebra.org/en/Reference:GeoGebra_Apps_API GeoGebra Apps API]:
 +
* [http://dev.geogebra.org/examples/html/example-api-multiple.html Showing & hiding objects with buttons]
 +
* [http://dev.geogebra.org/examples/html/example-api-save-state.html Saving & loading state]
 +
* [http://dev.geogebra.org/examples/html/example-api-listeners.html Listening to update, add, remove events]
  
<pre>    <div id="applet_container1"></div></pre>
+
=Offline and Self-Hosted Solution=
 +
We suggest you to use the GeoGebra Apps from our global and super-fast server network www.geogebra.org as shown above, but in case you prefer to host and update the GeoGebra apps yourself, you can [https://download.geogebra.org/package/geogebra-math-apps-bundle download our GeoGebra Math Apps Bundle]
 +
The embed codes are almost the same as above, with two differences: the tag for including deployggb.js needs to be changed to
 +
<pre>    <script src="GeoGebra/deployggb.js"></script></pre>
 +
and you need to include the following line before the inject() call:
 +
<pre>    applet.setHTML5Codebase('GeoGebra/HTML5/5.0/web3d/');</pre>
 +
Alternatively if you just need to fix to a specific version then you can still use the CDN like this (don't change the <code>5.0.</code> just the <code>498</code>)
 +
<pre>    applet.setHTML5Codebase("https://www.geogebra.org/apps/5.0.498.0/web3d");</pre>
 +
For technical reasons, you need to use <code>5.2</code> not <code>5.0</code> after version 804 eg
 +
<pre>    applet.setHTML5Codebase("https://www.geogebra.org/apps/5.2.804.0/web3d");</pre>
 +
 
 +
=Adjusting embedded apps appearance=
 +
This feature is currently available only for GeoGebra Notes. To change the colors of the UI elements, you can set some CSS variables
 +
<pre>
 +
<style>
 +
body {
 +
  --ggb-primary-color: #ee0290; /* used for most UI elements, including the toolbar header and buttons */
 +
  --ggb-primary-variant-color: #880061; /* used for floating buttons on hover */
 +
  --ggb-dark-color: #880061; /* used for highlighted text in dialogs */
 +
  --ggb-light-color: #f186c0; /* used for progress bar */
 +
}
 +
</style>
 +
</pre>
 +
 
 +
=Speed up loading time with a service worker=
 +
You can speed up loading of the GeoGebra library by using a service worker.
 +
To use a service worker you should set a specific version of GeoGebra using <code>setHTML5Codebase()</code>.
 +
 
 +
To install the service worker, first you have to include the ''sworker-locked.js''
 +
file under your domain (e.g. ''www.example.com/path/sworker-locked.js'').
 +
The service worker file can be found under the ''GeoGebra/HTML5/5.0/web3d/'' folder
 +
in the ''GeoGebra Math Apps Bundle''.
  
{{note|In case you are using the Math Apps Bundle, please change the first script tag to
+
Next, include the following snippet on the page where GeoGebra is loaded.  
<pre>    <script src="GeoGebra/deployggb.js"></script></pre>
+
Please change the path to the service worker. You can also set the appletLocation
and include the following two lines below the definition of applet2
+
variable to enable the worker only in a specific folder (or leave it as '/' to use it on all pages of your domain):
<pre>    applet1.setHTML5Codebase('GeoGebra/HTML5/5.0/web3d/');
+
 
     applet2.setHTML5Codebase('GeoGebra/HTML5/5.0/web3d/');</pre>}}
+
     var serviceWorkerPath = '/sworker-locked.js'
=Examples=
+
    var appletLocation = '/'
* [http://dev.geogebra.org/examples/html/example-single.html Single applet]
+
   
* [http://dev.geogebra.org/examples/html/example-3d.html Single applet 3D]
+
    function isServiceWorkerSupported() {
* [http://dev.geogebra.org/examples/html/example-multiple.html Two applets]
+
        return 'serviceWorker' in navigator && location.protocol === "https:";
* [http://dev.geogebra.org/examples/html/example-api.html Single applet + API]
+
    }
* [http://dev.geogebra.org/examples/html/example-api-multiple.html Two applets + API]
+
   
* [http://dev.geogebra.org/examples/html/example-api-save-state.html Single applet + API for saving]
+
    function installServiceWorker() {
* [http://dev.geogebra.org/examples/html/example-tools.html Apps with toolbar]
+
        if (navigator.serviceWorker.controller) {
* [http://dev.geogebra.org/examples/html/example-popup.html Apps with toolbar in a popup dialog]
+
            console.log("Service worker is already controlling the page.");
* [http://dev.geogebra.org/examples/html/example-api-listeners.html Event listeners]
+
        } else {
* [http://dev.geogebra.org/examples/html/example-api-sync.html Communication between applets]
+
            navigator.serviceWorker.register(serviceWorkerPath, {
* [http://dev.geogebra.org/examples/html/example-exercise.html Exercise (beta)]
+
                scope: appletLocation
 +
            });
 +
        }
 +
    }
 +
   
 +
    if (isServiceWorkerSupported()) {
 +
        window.addEventListener('load', function() {
 +
            installServiceWorker()
 +
        });
 +
    } else {
 +
        console.log("Service workers are not supported.");
 +
    }
  
[[es:Referencia:Applet Embebido]]
+
With this installed, when a user opens the page where the service worker is enabled,
[[fr:Référence:Appliquettes_imbriquées]]
+
the application scripts get downloaded and cached by the service worker. This way,
[[it:Riferimenti:Incorporare_Applet]]
+
the next time that same user visits the page, the scripts are loaded from the
 +
cache instead of downloading them again from the servers.

Revision as of 13:12, 27 September 2023

This page describes how to embed GeoGebra Apps into your website. For more information on how to interact with embedded apps, please see GeoGebra Apps API.

Quick Start

In order to embed GeoGebra apps into your page, you need to add the following three parts:

1 Make sure you have this in the <head> section to make sure scaling works correctly (eg on mobile browsers) and that Unicode works

<meta name=viewport content="width=device-width,initial-scale=1">  
<meta charset="utf-8"/>

2 The javascript library deployggb.js needs to be included with this tag:

<script src="https://www.geogebra.org/apps/deployggb.js"></script>

3 Create an element for the GeoGebra app on your page:

<div id="ggb-element"></div> 

4 Configure and insert the app:

<script>  
    var params = {"appName": "graphing", "width": 800, "height": 600, "showToolBar": true, "showAlgebraInput": true, "showMenuBar": true };
    var applet = new GGBApplet(params, true);
    window.addEventListener("load", function() { 
        applet.inject('ggb-element');
    });
</script>

Simply change the appName parameter from graphing to geometry or 3d to get one of our other apps. To load an activity you can use eg "material_id":"RHYH3UQ8" or "filename":"myFile.ggb" and you can customize our apps further by using various GeoGebra App Parameters.

Tutorial

Please see here for a longer tutorial which explains the different embedding options: https://www.geogebra.org/m/sehv2qc9

Live Examples

The following examples and their html source code show how to use GeoGebra apps either embedded in your page or in popup dialogs:

GeoGebra Apps API

The following examples show how you can interact with embedded apps through the GeoGebra Apps API:

Offline and Self-Hosted Solution

We suggest you to use the GeoGebra Apps from our global and super-fast server network www.geogebra.org as shown above, but in case you prefer to host and update the GeoGebra apps yourself, you can download our GeoGebra Math Apps Bundle The embed codes are almost the same as above, with two differences: the tag for including deployggb.js needs to be changed to

    <script src="GeoGebra/deployggb.js"></script>

and you need to include the following line before the inject() call:

    applet.setHTML5Codebase('GeoGebra/HTML5/5.0/web3d/');

Alternatively if you just need to fix to a specific version then you can still use the CDN like this (don't change the 5.0. just the 498)

    applet.setHTML5Codebase("https://www.geogebra.org/apps/5.0.498.0/web3d");

For technical reasons, you need to use 5.2 not 5.0 after version 804 eg

    applet.setHTML5Codebase("https://www.geogebra.org/apps/5.2.804.0/web3d");

Adjusting embedded apps appearance

This feature is currently available only for GeoGebra Notes. To change the colors of the UI elements, you can set some CSS variables

<style>
body {
  --ggb-primary-color: #ee0290; /* used for most UI elements, including the toolbar header and buttons */
  --ggb-primary-variant-color: #880061; /* used for floating buttons on hover */
  --ggb-dark-color: #880061; /* used for highlighted text in dialogs */
  --ggb-light-color: #f186c0; /* used for progress bar */
}
</style>

Speed up loading time with a service worker

You can speed up loading of the GeoGebra library by using a service worker. To use a service worker you should set a specific version of GeoGebra using setHTML5Codebase().

To install the service worker, first you have to include the sworker-locked.js file under your domain (e.g. www.example.com/path/sworker-locked.js). The service worker file can be found under the GeoGebra/HTML5/5.0/web3d/ folder in the GeoGebra Math Apps Bundle.

Next, include the following snippet on the page where GeoGebra is loaded. Please change the path to the service worker. You can also set the appletLocation variable to enable the worker only in a specific folder (or leave it as '/' to use it on all pages of your domain):

   var serviceWorkerPath = '/sworker-locked.js'
   var appletLocation = '/'
   
   function isServiceWorkerSupported() {
       return 'serviceWorker' in navigator && location.protocol === "https:";
   }
   
   function installServiceWorker() {
       if (navigator.serviceWorker.controller) {
           console.log("Service worker is already controlling the page.");
       } else {
           navigator.serviceWorker.register(serviceWorkerPath, {
               scope: appletLocation
           });
       }
   }
   
   if (isServiceWorkerSupported()) {
       window.addEventListener('load', function() {
           installServiceWorker()
       });
   } else {
       console.log("Service workers are not supported.");
   }

With this installed, when a user opens the page where the service worker is enabled, the application scripts get downloaded and cached by the service worker. This way, the next time that same user visits the page, the scripts are loaded from the cache instead of downloading them again from the servers.

© 2024 International GeoGebra Institute