8. Resources

When we refer to a resource, we mean any type of file that will be embedded with the widget. This can include images, sounds, PDF documents, JavaScript, CSS, XML files, and more.

In this chapter, we are going to create a CSS file, a JavaScript file, embed a video, and add them as resources to our widget. Let's get started!

Creating the CSS File

  1. Copy the following CSS content:

.tickle {
	animation: shake infinite 0.4s;
}

@keyframes shake {
	0% { transform: translate(0, 0); }
	10% { transform: translate(4px, -4px); }
	20% { transform: translate(-2px, 4px); }
	30% { transform: translate(1px, 2px); }
	40% { transform: translate(-4px, -4px); }
	50% { transform: translate(0, -1px); }
	60% { transform: translate(4px, -4px); }
	70% { transform: translate(-4px, 2px); }
	80% { transform: translate(-1px, -4px); }
	90% { transform: translate(4px, -4px); }
}
  1. Paste it into your favorite text editor.

  2. Save the file as "tickle.css" on your computer.

Creating the JavaScript File

  1. Copy the following JavaScript content:

function startTickle(videoElement) {
	videoElement.classList.add("wem-academy-tickle");
	videoElement.play();
}

function pauseTickle(videoElement) {
	videoElement.classList.remove("wem-academy-tickle");
	videoElement.pause();
}
  1. Paste it into your text editor.

  2. Save the file as "tickle.js."

Downloading the Video

  1. Download the video "tickle.mp4" that we are going to embed here.

Now that we have all the resources locally on our computer, let's create the widget.

Creating the Widget

  1. Create a new widget named "Tickle."

  2. Select the Resources tab.

  3. Click on "New resource."

  4. Name it "TickleCss."

A resource has been created, but without any file attached to it. In this case, we need to upload our newly created "tickle.css" file.

  1. Select the "TickleCss" resource if it is not already selected.

  2. Click the "Browse" button in the details panel on the right.

  3. Select the newly created "tickle.css" file on your computer.

  4. Click "OK."

The last three steps may vary slightly depending on the browser you are using.

The file should now be uploaded, and under the "File" column in the resources overview, the cell should display "tickle.css."

Now, repeat the process to create two more resources for "tickle.js" and "tickle.mp4," naming them "TickleJs" and "TickleVideo," respectively.

Adding the WEMscript

Almost there!

  1. Copy the HTML code below and paste it as a Script in the Widget Editor:

<? scriptreference "wem-academy-tickle" FileUrl(@TickleJs) ?>
<link rel="stylesheet" href="<?attr FileUrl(@TickleCss) ?>">
<video class="tickle" loop src="<?attr FileUrl(@TickleVideo) ?>" onmousemove="startTickle(this)" onmouseout="pauseTickle(this)"></video>

We introduce a new statement here called scriptreference, which requires two arguments. The first one should be a unique key (more on that in later chapters), and the second one is the URL of the resource. We also introduce the FileUrl function to retrieve the file URLs of all the resources.

Important Note: Every CSS and JavaScript file that you include in the widget will be globally interpreted in the browser. This means that if you are not careful, you could create CSS collisions or, even worse, overwrite JavaScript variables, functions, and classes! We will discuss this in more depth in later chapters.

For now, preview this widget, and hover over the video! :)

Last updated