Skip main navigation

Video Sensing extension

By using the Video Sensing extension, your Scratch project can react to the user's motion. Watch Mark demonstrate here.

In this step, you’ll use the Video Sensing extension so that users can interact with your Scratch projects. The Video Sensing extension allows you to overlay the video from your webcam onto the Stage, so that users can “reach towards” and interact with sprites.

Adding the Video Sensing extension blocks

When you add the Video Sensing extension blocks, the video stream from your webcam will appear on the Stage with sprites “on top” of your video. If you are using the online version, you may need to allow Scratch access to use your camera.

Video transparency

When the video stream appears on the Stage, the video stream will look partially see-through. This is because the transparency of the video stream is set to 50 percent by default. When the video transparency is set to 0 the video stream has no transparency — you cannot see the backdrop on the stage. When you set the transparency to 100 you can see the backdrop on the Stage and no image from your video stream.

To change the transparency of the video, you need to use the set video transparency to () block.

set video transparency to ()

The image below shows how the Stage area will appear with the transparency set at: 20, 40, 60 and 80.

A still from a video of Mark, shown at 20%, 40%, 60%, and 80% transparency. As the percentage increases, the image becomes fainter.

On and off

Although by default the video starts on, you may want to turn it off, and later turn it back on. For example, you may not want the video on while you present a welcome screen, or a final score. To do this, use the turn video () block.

turn video (on v)

Motion

Scratch will detect motion on the video. You can use this to start some code running using the when video motion > () block. If this code is on a sprite, Scratch will wait for motion on the video in the area hidden by the sprite, while if this code is on the Stage, Scratch will look for motion across the whole video.

To check if the motion is “significant” enough, Scratch will convert it into a value — the faster the motion, the higher the value. Scratch will then start to run your code when this value is greater than the number on the block. You can control the sensitivity by changing the number in the block: 10 is very sensitive — you do not have to create a lot of motion to run your code — while 100 is not very sensitive, so a lot of motion is needed to start your code.

This can be used to interact with a sprite. For example, the blocks below will make a “Meow” sound when the user creates motion behind the sprite.

(20), play sound (Meow v) until done”>

Motion and direction values

Scratch will also represent the direction of the motion it detects as a value. This direction value uses the same plane as sprite direction, so when you make a left to right horizontal motion on the video stream, the direction value will be 90.

You can use the video () on () block to get motion and direction values for a particular sprite or the entire Stage.

video (motion v) on (sprite v)

Follow the instructions below to create a project which uses the video () on () block to report the value of the motion on the Stage, and use this to set the volume of the sound that is playing.

  1. Open a new Scratch project
  2. Add the “chill” sound
  3. Add the Video Sensing extension blocks, allowing camera access if you need to
  4. Drag the blocks shown below to the Code area

    Two sequences of Scratch code. Sequence 1: when green flag clicked, forever, set volume to (video (motion v) on (stage v))%, end. Sequence 2: when green flag clicked, forever, play sound (chill v) until done, end.

  5. Click on the green flag to run your program
  6. Start dancing in front of your webcam

As you speed up your dance movements the volume will increase and as you slow your dance movements the volume will decrease.

Creating a balloon popping game

You can also use the Video Sensing extension blocks as part of an interactive game. Follow these instructions to create a balloon-popping game:

  1. Start a new project in Scratch
  2. Add the Video Sensing extension blocks, allowing camera access if you need to
  3. Position your camera so that your head and shoulders are on the Sprite Stage as shown below: Screenshot of the Scratch stage, with the Scratch cat sprite in the centre and a partially transparent image of Mark on the stage, with his torso in view
  4. Delete the Cat sprite
  5. Add the Balloon1 sprite to your project
  6. Drag-and-drop the blocks below into your Code area

(20) play sound (Pop v) until done, go to (random position v), next costume”>

Now, when motion happens behind the Balloon sprite, you will hear a popping sound and the Balloon1 sprite will move to a random position on the Stage and change colour.

Task

Choose one of the following ways to improve your balloon-popping game, and implement it:

  • Keep track of the user’s score
  • End the game after 60 seconds
  • Make the balloon glide around while the user is trying to catch it

Share your project on the Scratch website, and paste the share link in the comment section below. Look at the projects shared by your fellow learners, and reply to them with your comments.

This article is from the free online

Introduction to Programming with Scratch

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education