summaryrefslogtreecommitdiff
path: root/examples/script-file
diff options
context:
space:
mode:
Diffstat (limited to 'examples/script-file')
-rw-r--r--examples/script-file/Makefile.am27
-rw-r--r--examples/script-file/README24
-rw-r--r--examples/script-file/change-color.js13
-rw-r--r--examples/script-file/main.c51
-rw-r--r--examples/script-file/view.html13
5 files changed, 128 insertions, 0 deletions
diff --git a/examples/script-file/Makefile.am b/examples/script-file/Makefile.am
new file mode 100644
index 0000000..9c6cd12
--- /dev/null
+++ b/examples/script-file/Makefile.am
@@ -0,0 +1,27 @@
+
+AM_CFLAGS = -Wall -Wextra -Werror -std=gnu23
+AM_CPPFLAGS = -I$(top_srcdir)/src
+
+noinst_PROGRAMS = main
+noinst_DATA = example.svg
+
+main_SOURCES = main.c
+main_LDADD = $(top_builddir)/src/.libs/libsvgc.la
+
+EXTRA_DIST = change-color.js view.html
+CLEANFILES = example.svg
+
+example.svg: main $(srcdir)/change-color.js
+ @if test "$(srcdir)" != "."; then \
+ cp "$(srcdir)/change-color.js" change-color.js; \
+ cp "$(srcdir)/view.html" view.html; \
+ fi
+ ./main
+ @if test "$(srcdir)" != "."; then \
+ rm -f change-color.js; \
+ fi
+
+clean-local:
+ @if test "$(srcdir)" != "."; then \
+ rm -f view.html; \
+ fi
diff --git a/examples/script-file/README b/examples/script-file/README
new file mode 100644
index 0000000..8f10bdf
--- /dev/null
+++ b/examples/script-file/README
@@ -0,0 +1,24 @@
+SVGC interactive-script example
+===============================
+
+This example demonstrates __svg_sheet_add_script_file().
+
+The user writes only change-color.js as plain UTF-8 JavaScript source.
+The C application registers that file with the SVG sheet; SVGC reads and
+copies it into a <script type="text/javascript"><![CDATA[...]]></script>
+definition when serializing the SVG.
+
+After installing SVGC:
+
+ make
+
+Open example.svg directly as an SVG document, or use view.html, which
+embeds it with <object type="image/svg+xml">. Click the red circle: the
+script changes its fill to blue. Loading the SVG with <img> deliberately
+does not execute scripts in modern browsers. Opening local files may also
+be affected by browser security settings; a local HTTP server is an
+alternative for manual testing when the browser restricts file://.
+
+No binary scripts or external dependencies are bundled with the SVG.
+No JavaScript parser is part of the SVGC library; the viewer executes
+any embedded source in its normal security context.
diff --git a/examples/script-file/change-color.js b/examples/script-file/change-color.js
new file mode 100644
index 0000000..e831b22
--- /dev/null
+++ b/examples/script-file/change-color.js
@@ -0,0 +1,13 @@
+/* JavaScript source only: do NOT add <defs>, <script> or CDATA tags. */
+
+function changeColor(event)
+{
+ event.target.style.fill = 'blue';
+}
+
+document.addEventListener('DOMContentLoaded', function ()
+{
+ var circle = document.querySelector('circle');
+ if (circle)
+ circle.addEventListener('click', changeColor);
+});
diff --git a/examples/script-file/main.c b/examples/script-file/main.c
new file mode 100644
index 0000000..936916f
--- /dev/null
+++ b/examples/script-file/main.c
@@ -0,0 +1,51 @@
+
+/***************************************************************
+ SVGC EXAMPLE: EMBEDDED ECMASCRIPT
+
+ The file change-color.js contains only JavaScript source
+ text. Interactive script execution requires opening the SVG
+ as a document.
+ ***************************************************************/
+
+#include <stdio.h>
+#include <svgc.h>
+
+
+int
+main( void )
+{
+ struct __svg_sheet *sheet;
+ struct __svg_scene *scene;
+ struct __svg_node *circle;
+
+ sheet = (struct __svg_sheet *)__svg_new_sheet( 520, 220, 16, 16, 16, 16 );
+ if( !sheet ) return( 1 );
+
+ if( __svg_sheet_add_script_file( sheet, "change-color.js" ) != 1 )
+ {
+ __svg_free_node( sheet );
+ fprintf( stderr, "Cannot add change-color.js to the SVG sheet\n" );
+ return( 1 );
+ }
+
+ scene = __svg_new_scene( sheet );
+ if( !scene )
+ {
+ __svg_free_node( sheet );
+ return( 1 );
+ }
+
+ circle = __svg_new_circle( 260, 110, 72 );
+ if( !circle ||
+ __svg_set_fill( circle, "red" ) != 1 ||
+ __svg_scene_add_node( scene, circle ) != 1 )
+ {
+ if( circle ) __svg_free_node( circle );
+ __svg_free_scene( scene );
+ return( 1 );
+ }
+
+ __svg_paint_scene( scene, 0, "example.svg" );
+ __svg_free_scene( scene );
+ return( 0 );
+}
diff --git a/examples/script-file/view.html b/examples/script-file/view.html
new file mode 100644
index 0000000..5494951
--- /dev/null
+++ b/examples/script-file/view.html
@@ -0,0 +1,13 @@
+<!doctype html>
+<html lang="en">
+<head>
+ <meta charset="utf-8">
+ <title>SVGC: Embedded Script</title>
+</head>
+<body>
+ <p>Click the red circle to change it to blue.</p>
+ <object data="example.svg" type="image/svg+xml" width="520" height="220">
+ <p>Open example.svg as a standalone SVG document.</p>
+ </object>
+</body>
+</html>