Document Object Model (DOM) =========================== Psychex offers classes to interact with the DOM, allowing you to build HTML elements alongside your canvas elements! This helps contain all your needs within the Psychex framework, and can be used to incorporate forms, and questionnaires into your experiments. This page contains tutorials showing how to create a Psychex form. Further down the page are the details on the methods available for each of the classes. Create a Custom Form - Tutorial ------------------------------- The following tutorial shows how you can build a custom form from scratch, using Psychex versions of normal HTML elements. The purpose of this tutorial is to show how you can define, position, and style different elements using Psychex. The final form you'll produce is perfectly usable for an experiment - but may have some limitations if you wanted something more complex. By the end of the tutorial, you'll be able to confidently use elements like checkboxes, sliders, text, and input fields to get data from users. This can be extremely useful for implementing attention checks, inter-round feedback, or quick responses when using a mixture of canvas and DOM elements, without having to redirect to a new page, or go through complex positioning in your HTML. Start by downloading the `starter_kit` folder from the Github repo. Open up the entrypoint file: `static/js/main.js`. This contains definitions of the core functions you need to get started. The first thing we'll do is define a custom form class. Define this at the bottom of the page: .. code-block:: javascript class MyTutorialForm{ constructor(x, y){ } } This takes coordinates `x` and `y` as input. Psychex DOM elements work in a different way to canvas elements, in that they don't need to be redrawn continuously in an animation loop - **they only need to be drawn once**. This means that when we define a new element, it is registered in the DOM, and kept on the page permanently (unless manually removed). Let's start by defining a new *div*, and placing a *heading* and *subheading* inside it. We'll make the headings *children* of the *div*, so that they're positioned relative to its position. This is exactly the same as if we were writing pure HTML, and we nested *
element. :param number x: Horizontal position of the element :param number y: Vertical position of the element :param string value: The text content of the string. Accepts html and rich text. :param string id: Unique identifier string for this element :param object kwargs: CSS styles for this element .. js:class:: Input(x, y, value="", id=undefined, kwargs={}) Create a new HTML element. :param number x: Horizontal position of the element :param number y: Vertical position of the element :param string value: The starting value of the input. Can be set to "" to have an empty value. :param string id: Unique identifier string for this element :param object kwargs: CSS styles for this element .. js:method:: onInput(callback) Provide a callback that runs when data is input to this element. This is reactive, and runs each time there is a change. :param function callback: A callback to be run on each input - i.e. each time a key is typed while the box is active. :return: Callback return :rtype: any .. js:method:: setPlaceholder(t) Set the placeholder text for the input (if applicable). For instance, in a text input, this might be the text shown on instantation before the user starts typing, and disappears when the input is interacted with. :param string t: The placeholder text :return: this object reference :rtype: object .. js:method:: getPlaceholder(t) Return the current placeholder text :return: the placeholder text :rtype: string .. js:method:: clear() Clear the value of the current input. Useful for when building forms. :return: this object reference :rtype: object .. js:class:: Button(x, y, value, id=undefined, kwargs={}) Create a new HTML