Graphics
********

A "GraphicPrompt" shows an animated graphic in a "ModularPage", and a
"GraphicControl" shows one that the participant answers by clicking.
PsyNet draws graphics with the JavaScript library Raphaël, whose
reference documents the attributes that objects accept.

The examples on this page come from the "demos/experiments/graphics"
demo.


Drawing frames
==============

A graphic is a list of "Frame" objects, shown in sequence. Each frame
contains "GraphicObject" instances that are drawn together: "Text",
"Image", "Path", "Circle", "Ellipse" and "Rectangle". Each object
takes an ID, which must be a valid variable name.

"dimensions" sets the coordinate system in which objects are placed.
It also sets the aspect ratio, but not the size on screen:
"viewport_width" sets the width as a fraction of the browser window
(default "0.6"). "attributes" sets SVG attributes such as "fill" or
"font-size"; the Raphaël attribute reference lists them.

   prompt=GraphicPrompt(
       text="This GraphicPrompt illustrates some of the different kinds of geometric objects.",
       dimensions=[100, 100],
       viewport_width=0.5,
       frames=[
           Frame(
               [
                   Path(
                       "triangle",
                       "M50,10 L10,50 L90,50 z",
                       attributes={"fill": "red"},
                   ),
                   Circle(
                       "circle",
                       20,
                       70,
                       radius=5,
                       attributes={"fill": "blue"},
                       animations=[
                           Animation(
                               {"cx": 80}, duration=0.7, easing="bounce"
                           ),
                           Animation(
                               {"cx": 20}, duration=1.2, easing="ease-out"
                           ),
                       ],
                       loop_animations=True,
                   ),
                   Ellipse(
                       "ellipse",
                       80,
                       80,
                       radius_x=10,
                       radius_y=5,
                       attributes={"fill": "yellow"},
                   ),
                   Rectangle(
                       "rectangle",
                       50,
                       85,
                       width=80,
                       height=5,
                       attributes={"fill": "black"},
                   ),
               ]
           )
       ],
   ),

A frame lasts "duration" seconds, or indefinitely if "duration" is
"None" (the default). Set "loop=True" on the prompt or control to
return to the first frame after the last one. An object with
"persist=True" stays on screen in later frames.


Clickable objects
=================

In a "GraphicControl", objects created with "click_to_answer=True"
submit the page when clicked. The answer is a dictionary containing
"clicked_object", the ID of the object, and "click_coordinates", the
position of the click. This page combines a graphic prompt with a
graphic control:

   prompt=GraphicPrompt(
       text="This page contains both a GraphicPrompt and a GraphicControl.",
       dimensions=[300, 100],
       viewport_width=0.6,
       frames=[
           Frame(
               [Text("question", "What's up? Click to answer.", x=150, y=50)]
           )
       ],
   ),
   control=GraphicControl(
       dimensions=[300, 100],
       viewport_width=0.6,
       frames=[
           Frame(
               [
                   Text(
                       "not_much",
                       "Not much.",
                       x=150,
                       y=20,
                       click_to_answer=True,
                   ),
                   Text("lots", "Lots.", x=150, y=80, click_to_answer=True),
               ]
           )
       ],
   ),


Animation
=========

An "Animation" moves an object from its initial attributes to
"final_attributes" over "duration" seconds, with an optional "easing".
An object's "animations" list runs in order, and
"loop_animations=True" repeats it. Circles and ellipses are positioned
with "cx" and "cy" rather than "x" and "y".

Images must be listed in the "media" argument, which loads them once
before the graphic is drawn. Objects refer to them by "media_id":

   control=GraphicControl(
       dimensions=[100, 100],
       viewport_width=0.5,
       frames=[
           Frame(
               [
                   Text(
                       "title",
                       "PsyNet is great!",
                       x=50,
                       y=20,
                       click_to_answer=True,
                       persist=True,
                       attributes={"fill": "blue"},
                       animations=[
                           Animation({"fill": "red"}, duration=0.2),
                           Animation({"fill": "blue"}, duration=0.2),
                       ],
                       loop_animations=True,
                   ),
                   Image(
                       "logo",
                       media_id="logo",
                       x=10,
                       y=40,
                       width=75,
                       click_to_answer=True,
                       animations=[
                           Animation(
                               {"x": 5, "y": 45, "width": 60}, duration=0.25
                           ),
                           Animation(
                               {"x": 15, "y": 50, "width": 90}, duration=0.25
                           ),
                           Animation(
                               {"x": 5, "y": 55, "width": 60}, duration=0.25
                           ),
                           Animation(
                               {"x": 10, "y": 60, "width": 76}, duration=0.25
                           ),
                       ],
                   ),
               ],
               duration=1,
           ),
           Frame(
               [
                   Image(
                       "logo",
                       media_id="logo",
                       x=10,
                       y=60,
                       width=75,
                       click_to_answer=True,
                       animations=[
                           Animation(
                               {"y": 40, "transform": "r180"}, duration=0.5
                           ),
                           Animation(
                               {"y": 40, "transform": "r0"}, duration=0.5
                           ),
                       ],
                   )
               ],
               duration=1,
           ),
       ],
       loop=True,
       media=MediaSpec(image=dict(logo="/static/logo.svg")),
   ),


Audio
=====

A frame's "audio_id" plays a sound from "media" when the frame starts:

   prompt=GraphicPrompt(
       text="This GraphicPrompt has synchronized audio.",
       dimensions=[100, 100],
       viewport_width=0.5,
       frames=[
           Frame(
               [
                   Text(
                       "title",
                       "Beer!",
                       x=50,
                       y=30,
                       attributes={"fill": "blue"},
                   )
               ],
               duration=1,
               audio_id="bier",
           ),
           Frame(
               [
                   Text(
                       "title", "Beer!", x=50, y=60, attributes={"fill": "red"}
                   )
               ],
               duration=1,
               audio_id="bier",
           ),
       ],
       loop=True,
       media=MediaSpec(audio={"bier": "/static/bier.wav"}),
   ),


Timing a control from a graphic
===============================

With "prevent_control_response=True", the page's control stays
inactive until a frame with "activate_control_response=True" is
reached. "prevent_control_submit" and "activate_control_submit" do the
same for submitting. This page counts down, then starts recording:

   prompt=GraphicPrompt(
       text="This example shows how the GraphicPrompt can be used to trigger timing in the Control object.",
       dimensions=[100, 100],
       viewport_width=0.4,
       frames=[
           Frame([Text("num_3", "3", 50, 50)], duration=1),
           Frame([Text("num_2", "2", 50, 50)], duration=1),
           Frame([Text("num_1", "1", 50, 50)], duration=1),
           Frame(
               [Text("sing", "Sing!", 50, 50)],
               duration=2,
               activate_control_response=True,
           ),
           Frame([Text("stop", "Stop.", 50, 50)], duration=None),
       ],
       prevent_control_response=True,
   ),
   control=AudioRecordControl(
       duration=2,
       bot_response_media="example_recordings/example_recording.wav",
   ),


Pages with only a graphic
=========================

"GraphicPage" is a shortcut for a "ModularPage" with an empty prompt
and a "GraphicControl". It takes a label, a "time_estimate", and the
arguments of "GraphicControl", such as "auto_advance_after" for
graphics that advance by themselves.

See also:

  Graphics exercise and Graphics.

  The Develop experiment front end and Psychophysics experiments
  skills.
