简体   繁体   English

使用Canvas和HTML5创建类似动画的flash

[英]Creating flash like animations with Canvas and HTML5

This is a "where can I start" type question. 这是一个“我在哪里可以开始”类型的问题。

I'm in process of learning HTML5 and Canvas work but have a feeling that I'm looking in the wrong area. 我正在学习HTML5和Canvas的工作,但感觉我正在寻找错误的领域。

I would like to learn how to create cartoon type flash-like responsive animations. 我想学习如何创建卡通类型的闪存响应动画。 Imagine this teddy bear: 想象一下这只泰迪熊: 泰迪熊图像

When I point my mouse at it I want to make him walk across the screen by implementing my "moving feet" animations etc..When click, I want him to wave his paw. 当我将鼠标指向它时,我想通过实现我的“移动脚”动画等让他走过屏幕。点击时,我希望他挥动他的爪子。

With HTML5 and Javascript I can make him move/float across but I can't find the way to actually ANIMATE the movements. 使用HTML5和Javascript我可以让他移动/浮动,但我找不到实际动画移动的方法。

Do I create small .mp4 files? 我是否创建了小.mp4文件? Do I create a bunch of images to loop through them? 我是否创建了一堆图像来循环它们? Animated GIFs? 动画GIF? I would like to stay away from flash ofcourse... 我想远离闪光灯......

I thought HTML5 with Canvas animation would allow me to achieve what I want but other than drawing simple shape animations and Video work I can't seem to find tutorials or "How to" articles. 我认为带有Canvas动画的HTML5可以让我实现我想要的效果,但除了绘制简单的形状动画和视频作品外,我似乎无法找到教程或“如何”文章。

How can I achieve what I'm trying to do or do I need to look elsewhere? 我怎样才能实现我想要做的事情或者我需要去其他地方看看? I would appreciate being pointed in the right direction. 我希望被指向正确的方向。

Edit: I ran into the following game while doing research: http://www.cuttherope.ie/ How, for example is the monster animated in something like this? 编辑:我在做研究时遇到了以下游戏: http//www.cuttherope.ie/如何,例如怪物动画在这样的东西?

Canvas will do you well here. 帆布在这里对你有好处。

Animation is typically done merely by cycling through different PNG files (or a spritemap with different images). 动画通常仅通过循环遍历不同的PNG文件(或具有不同图像的精灵图)来完成。 Here's a jsfiddle example I did not long ago that shows a simple animation on the canvas: 这是我不久前在画布上显示简单动画的jsfiddle示例

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

var image = new Image();

var drawTile = function(x, y, tile, width, height) {
    context.drawImage(image, (tile % 6) * width, Math.floor(tile / 6) * height, width, height, x, y, width, height);
};

image.onload = function() {
    // draw all 12 tiles:
    var i = 0;
    setInterval(function() {
        context.clearRect(0,0,500,500);
        drawTile(0, 0, i, 48, 32);
        i++;
        if (i > 11) i = 0;
    }, 85);
}
image.src = 'http://i.stack.imgur.com/qkWe2.png';

If you look at the image in the example, you get an idea of what a sprite map for animation looks like: 如果你看一下例子中的图像,你就会知道动画的精灵地图是什么样的:

在此输入图像描述

Cycling thought the sprites in such an image based on a timer is all that's needed! 骑自行车认为基于计时器的精灵中的精灵就是所需要的!


Making and moving clickable, selectable objects on the canvas takes a bit of footwork. 在画布上制作和移动可点击的可选对象需要一些步法。 There's nothing built in for object detection on the canvas. 在画布上没有内置任何对象检测功能。 But if you've done programming before it isn't too hard, and I've written a popular tutorial on the matter that serves as a very gentle introduction. 但是,如果你在编写之前已经完成了编程,那么我就已经编写了一篇关于这个问题热门教程 ,作为一个非常温和的介绍。 It should help you out. 它应该会帮助你。

Other than that, your best resource is, of course, Stack Overflow. 除此之外,您最好的资源当然是Stack Overflow。 :) We'll always be glad to answer individual questions as you come across them. :)当你遇到他们时,我们总是很乐意回答个别问题。

If the purpose of the exercise is to learn HTML5 and canvas then the following is probably not for you. 如果练习的目的是学习HTML5和画布,那么下面的内容可能不适合你。 If you need to create your teddy animation as quickly and easily as possible, and deliver it using standards based technologies, then I would suggest you download and explore the preview of Adobe Edge . 如果您需要尽可能快速,轻松地创建泰迪动画,并使用基于标准的技术提供它,那么我建议您下载并浏览Adobe Edge的预览。 It's a motion and interaction design tool similar to Flash (timeline and keyframes) which outputs a combination of HTML, CSS and JavaScript. 它是一个类似于Flash(时间轴和关键帧)的运动和交互设计工具,它输出HTML,CSS和JavaScript的组合。 I haven't played with the preview version but I'm pretty sure it will be capable of producing the sort of animation you're talking about. 我没有使用预览版本,但我很确定它能够产生你正在谈论的那种动画。

Update: 更新:

You might also want to consider Zoë which can be used to export SWF animations directly to sprite sheets. 您可能还需要考虑可以用于将SWF动画直接导出到精灵表的Zoë It was created by the team which developed the Canvas library easel.js and can export the accompanying frame data as either JSON or easel.js. 它是由开发Canvaseasel.js的团队创建的,可以将附带的帧数据导出为JSON或easel.js。 It will allow you to create your animations in Flash (which, let's face it, is still the best web animation tool around) but render them using HTML. 它将允许您在Flash中创建动画(让我们面对它,仍然是最好的Web动画工具),但使用HTML渲染它们。

For animations, three.js is nice. 对于动画, three.js很不错。 It's even got a few physics engine plugin s. 它甚至还有一些物理引擎插件

Also, take a look at parts of HTML5 Game Development 另外,看看HTML5游戏开发的部分内容

I'll quickly bow to a more educated answer, but I would guess what you want to do may be a little too much for canvas/javascript performance in the browser. 我会很快向一个更有教养的答案屈服,但我猜你想要做什么可能对浏览器中的canvas / javascript性能有点太多了。

If you're an animator by trade it might not seem like such a tedious task, but I imagine you would be writing a lot of code to animate intricate paths and fill them and etc... 如果你是一个动画制作者,它可能看起来不是那么乏味的任务,但我想你会写很多代码来制作错综复杂的路径并填充它们等等......

I'm sure you've researched, but there are numerous javascript canvas libraries out there. 我相信你已经研究过了,但是那里有很多javascript画布库。 Paper js comes to mind or processing js . 想到纸张js处理js They might point you in a decent direction if you check out their examples/capabilities concerning paths and animation. 如果你查看他们关于路径和动画的例子/能力,他们可能会指出你一个体面的方向。

The way I see it, there are two problems to tackle: 我看待它的方式,有两个问题需要解决:

  1. Give the bear motion within itself (legs at a minimum) 让熊内部运动(腿至少)
  2. Move the bear across the screen 将熊移过屏幕

Tackling these two gets you the appearance of the bear walking across the screen (as opposed to just moving across the screen or walking in place). 解决这两个问题会让你看到熊在屏幕上行走的外观(而不是只是在屏幕上移动或走在原地)。 It sounds like you've already tackled problem #1. 听起来你已经解决了问题#1。

To tackle #2, your best bet is to have an image per-frame. 为了解决#2,你最好的选择是每帧都有一个图像。 Trying to draw the bear in a different position for each frame is a waste of time. 试图在每个帧的不同位置绘制熊是浪费时间。 If you preload the frame images, you can take the cost once at the beginning and them you're just displaying a different image as he walks. 如果您预先加载帧图像,您可以在开始时花费一次成本,而他们只是在走路时显示不同的图像。

Now, is this a job for canvas? 现在,这是帆布的工作吗? I don't know. 我不知道。 I'm thinking you could probably build what I've described in "old school" HTML/JavaScript/CSS. 我想你可能会构建我在“老派”HTML / JavaScript / CSS中所描述的内容。 The platform is up to you--but frames--and more specifically an image for each frame--are the key concept here. 平台取决于您 - 但帧 - 而且更具体地说是每帧的图像 - 是这里的关键概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM