search
HomeWeb Front-endH5 TutorialQuick start createjs example tutorial

Quick start createjs example tutorial

Jun 21, 2017 am 10:33 AM
createjsjavascriptgetting Startedfastarticle

When I started using the createjs framework, I found that there were still very few related tutorials on the Internet, so I wrote an article for easy reference in the future.
createjs introduction
Official website: http://www.createjs.cc/
createjs contains the following four parts:
EaselJS: used for drawing Sprites, animations, vectors and bitmaps, creating interactive experiences on HTML5 Canvas (including multi-touch)
TweenJS : Used for animation effects
SoundJS: Audio playback engine
PreloadJS: Website resource preloading
Similar to SoundJS, PreloadJS, if you handle it yourself If it is more convenient, you can also write them yourself. Generally speaking, they are equivalent to an auxiliary function, optional or not. Therefore, this article mainly explains the use of EaselJS.
1. The general api of EaselJS
  • Draw pictures Use (Bitmap)

  • to draw graphics, such as rectangles, circles, etc. Use (Shape) [Similar to changing coordinates x, y, increasing shadow, transparency alpha, reducing and enlarging scaleX/scaleY You can do it]

  • Draw text, use (Text)

  • There is also the concept of container Container, the container can contain multiple display objects

2. The general process of EaselJS drawing
##The general process:
Create a display object→Set some parameters→Call method drawing→Add to stage→update(), the code is as follows:
<script></script>  //引入相关的js文件
<canvas></canvas> canvas = document.querySelector('#canvas' stage =  rect = rect.graphics.beginFill("#f00").drawRect(0, 0, 100, 100stage.update();
graphics can set some styles, Line width, color, etc., you can also call some methods to draw graphics, such as rectangle drawRect, circle drawCircle, etc. You can check the API yourself for details.

Note: Remember to add the shape object to the stage, otherwise it will not be displayed on the screen.
3. Ticker timer
Write createjs for sure One you will encounter is ticker, which mainly refreshes the stage regularly. The ideal frame rate is 60FPS
createjs.Ticker.setFPS(60);

4 . Control the hierarchical relationship of multiple display objects
stage. The contain object has a children attribute that represents the child elements. It is an array. The element level inside starts from 0 like a subscript. Simple In other words, the latter one overwrites the previous one, and the addChild method is added to the end of the display list.
We can also dynamically change the cascading effect of children.
stage.setChildIndex(red,1);

5. Container container
It can contain Text, Bitmap, Shape, Sprite and other EaselJS elements are included in a Container to facilitate unified management.
For example, a character is composed of hands, feet, head, and body. You can put these parts in the same container and move them uniformly. The method of use is also relatively simple:
var contain = new createjs.Container(); 
contain.addChild(bgImg);
contain.addChild(bitmap);  
stage.addChild(contain);

嬹嬷嬬~The focus of this article is to draw images and process them

6. Draw pictures
var bg = new createjs.Bitmap("./background.png");
stage.addChild(bg);
stage.update();
According to the normal drawing process of EaselJS above, the above The code snippet should display normally. However, it can only be displayed normally in some cases. This image resource needs to be loaded successfully before it can be new. Otherwise, there will be no image on the canvas. If there is resource preloading, you can use the above code directly. If not, then It needs to be drawn after the image is loaded onload

var img = new Image();
img.src = './img/linkgame_pass@2x.png';
img.onload = function () { var bg = new createjs.Bitmap("./background.png");
 stage.addChild(bg);
 stage.update();
}
Just drawing pictures is not enough. Createjs provides several methods for processing pictures:

6.1 Add a mask layer to the image
Use the mask attribute to display only the area where the image intersects with the shape
stage = new createjs.Stage("gameView");
bg = new createjs.Bitmap("./img/linkgame_pass@2x.png");
bg.x = 10;
bg.y = 10;//遮罩图形shape = new createjs.Shape();
shape.graphics.beginFill("#000").drawCircle(0, 0, 100);
shape.x = 200;
shape.y = 100;
bg.mask = shape;     //给图片bg添加遮罩stage.addChild(shape);
stage.addChild(bg);
stage.update();
Common application scenarios: used to crop pictures, such as displaying circular pictures, etc.

6.2 Add filter effects to pictures

var blur = new createjs.BlurFilter(5,5,1);
bg.filters = [blur];
We found that the picture still did not become blurred. The reason is that the stage is refreshed immediately after adding a filter to the picture. The filter can only maintain the effect of one frame, and the second frame filter failed. After using the cache() method of the image, the filter effect can be maintained no matter how the stage is refreshed. Adding cache has many effects, such as improving FPS, caching, etc.
bg.cache(0,0,bg.image.width,bg.image.height);

6.3 Use Rectangle to crop the imageUse the built-in Rectangle object of EaselJS to create a selection box to display certain parts of the image.
stage = new createjs.Stage("gameView");
bg = new createjs.Bitmap("./img/linkgame_pass@2x.png");
bg.x = 10;
bg.y = 10;var rect = new createjs.Rectangle(0, 0, 121, 171);
bg.sourceRect = rect;
stage.addChild(bg);
stage.update();
适用场景:拼图小游戏,剪裁图片……

 

7. createjs事件

easeljs事件默认是不支持touch设备的,需要以下代码才支持:

createjs.Touch.enable(stage);

对于Bitmap,Shape等对象,都可以直接使用addEventListener进行事件监听

bitmap = new createjs.Bitmap('');
bitmap.addEventListener(‘click’,handle);

 

8. CreateJs的渲染模式
CreateJs提供了两种渲染模式,一种是用setTimeout,一种是用requestAnimationFrame,默认是setTimeout,默认的帧数是20,一般的话还没啥,但是如果动画多的话,设置成requestAnimationFrame模式的话,就会感觉到动画如丝般的流畅。
 
 
9.适配
在移动端开发中,不得不面对一个多屏幕,多尺寸的问题,所以适配问题显得特别重要。
<canvas></canvas>

注意,以上代码的width,height不同于css中的width,height。

比如,你在canvas内部绘制图片,用x,y轴进行定位,这里的x,y是相对于canvas这个整体。

我们再把canvas当成一整张图片使用css进行适配

canvas{
     width: 100%;
}

那么,就会有以下的效果,canvas会适配屏幕尺寸,里面的图片也会等比例变大变小。

     

 

 

The above is the detailed content of Quick start createjs example tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
H5 Code Examples: Practical Applications and TutorialsH5 Code Examples: Practical Applications and TutorialsApr 25, 2025 am 12:10 AM

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

The Connection Between H5 and HTML5: Similarities and DifferencesThe Connection Between H5 and HTML5: Similarities and DifferencesApr 24, 2025 am 12:01 AM

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

The Building Blocks of H5 Code: Key Elements and Their PurposeThe Building Blocks of H5 Code: Key Elements and Their PurposeApr 23, 2025 am 12:09 AM

Key elements of HTML5 include,,,,,, etc., which are used to build modern web pages. 1. Define the head content, 2. Used to navigate the link, 3. Represent the content of independent articles, 4. Organize the page content, 5. Display the sidebar content, 6. Define the footer, these elements enhance the structure and functionality of the web page.

HTML5 and H5: Understanding the Common UsageHTML5 and H5: Understanding the Common UsageApr 22, 2025 am 12:01 AM

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

HTML5: The Building Blocks of the Modern Web (H5)HTML5: The Building Blocks of the Modern Web (H5)Apr 21, 2025 am 12:05 AM

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

H5 Code: Writing Clean and Efficient HTML5H5 Code: Writing Clean and Efficient HTML5Apr 20, 2025 am 12:06 AM

How to write clean and efficient HTML5 code? The answer is to avoid common mistakes by semanticizing tags, structured code, performance optimization and avoiding common mistakes. 1. Use semantic tags such as, etc. to improve code readability and SEO effect. 2. Keep the code structured and readable, using appropriate indentation and comments. 3. Optimize performance by reducing unnecessary tags, using CDN and compressing code. 4. Avoid common mistakes, such as the tag not closed, and ensure the validity of the code.

H5: How It Enhances User Experience on the WebH5: How It Enhances User Experience on the WebApr 19, 2025 am 12:08 AM

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.

Deconstructing H5 Code: Tags, Elements, and AttributesDeconstructing H5 Code: Tags, Elements, and AttributesApr 18, 2025 am 12:06 AM

HTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor