Quantcast
Channel: Pixi.js Latest Topics
Viewing all 3978 articles
Browse latest View live

Proper way to animate sprites

$
0
0

I'm wondering what the best way to animate a sprite is. Is there a basic example or tutorial out there somewhere?

I have ten images in a spritesheet that are part of an idling animation, and I made a texture for each of the images. I put the textures in an array, and my plan is to just change the texture of my sprite each frame in the gameloop, looping through the array of textures over and over again. Maybe using Sprite.setTexture ???

Is that a good way to go about it? Any links to info or tutorials would be much appreciated.

Thanks,


DisplayObject cursor property not working in CEF

$
0
0

Hi,

I just updated an old version of pixi.js we had been using. 

The cursor used to change to "pointer" when hovering over a DisplayObject in our embedded chromium browser (CEF).

After v4.4.0 the cursor does not change on hover.

I pinpointed that the last version it was working with our embedded browser was v4.3.5. Does anyone know why this might be and what I can do to fix it? 

Thank you

Need advice for some project

$
0
0

Hi guys. I want to create a effect in PIXI, that looks like first frames in this video -> http://hubblesite.org/video/513/news_release/2006-01 (stars shooting/flying by). What would more experienced PIXI'oniers advice to achieve such effect? Performance is critical, I want it also to work on mobiles.

For now I'm thinking:

1) Particle container, that's for sure? Maybe with some 1-3 star sprites?

2) Projection plane for start movement? Or just calculate it with some custom simple logic?

 

AnimatedSprite single pass

$
0
0

Hi,

I was trying to make my AnimatedSprite run only once. Initially I thought it could be achieved by setting `loop` to `false` and manually calling `play()`. However that worked only first time. For instance, I wrote code to run the animation once when mouseover. Later on when the animation is over I would take my mouse elsewhere and then bring it back over the sprite to trigger animation again, but this time it wouldn't play. After lot of experimentation I figured out that I have to call `gotoAndStop(0)` in the `onComplete` callback to achieve what I wanted.


let animSprite;

// Load animSprite

animSprite.loop = false;
animSprite.gotoStop(0);

animSprite.onComplete = function () {
  animSprite.gotoAndStop(0);
};

animSprite.play();

Is this the right way to achieve single-pass animation?

Backdrop filter in Pixi?

$
0
0

Hello,

I'm investigating some web-gl solutions that would allow an effect similar to Safari's CSS backdrop filter. I know Pixi supports creating custom filters, but this filter is usually a little bit trickier as it involves consideration of the texture data behind the actual object/node that has the filter. e.g.

https://webkit.org/demos/backdrop-filter/

Curious if anyone has investigated this with Pixi. Is this something that can be reasonably accomplished with Pixi? Rough sketches of approach would be appreciated, assuming someone hasn't already cracked this nut. 

Thanks! 

-D

Box2D examples

$
0
0

I am an experienced programmer but brand new to game dev.  Are there any good Box2D examples for Pixi?

Perspective correct texture mapping

$
0
0

I have opened the same question on stackoverflow: https://stackoverflow.com/questions/47342853/perspective-correct-texture-mapping-in-pixi-js

My purpose is making a simple 3d engine with 2D sprite (I choose Pixi.js this time as I can control the vertices of sprite arbitrarily), but I cannot understand how to map the texture to a perspective projected quad correctly. 
texture: https://imgur.com/Z5xDL8V 
I am using the PIXI.mesh.Plane for the quad, if the subdivision is low

var verticesX = 2, verticesY = 2;
this.field = new PIXI.mesh.Plane(resources['palette'].texture, verticesX , verticesY );

This will give out the result of screenshot01: https://imgur.com/a/m261h 
You can see the texture mapping is incorrect. 

Goes on I increase the division to 4 after the projection to screen vertices, and calculate the division point linearly.

this.field = new PIXI.mesh.Plane(resources['palette'].texture, 5, 5);
var lt = [this.field.model.screenVertices[0], this.field.model.screenVertices[1]];
var rt = [this.field.model.screenVertices[2], this.field.model.screenVertices[3]];
var rb = [this.field.model.screenVertices[4], this.field.model.screenVertices[5]];
var lb = [this.field.model.screenVertices[6], this.field.model.screenVertices[7]];
var segX = this.field.verticesX - 1;
var segY = this.field.verticesY - 1;
var vertices = [];
for (var i = 0; i < (segY + 1); i++) {
  var l = (lb[0] - lt[0]) / segY * i + lt[0];
  var r = (rb[0] - rt[0]) / segY * i + rt[0];
  for (var j = 0; j < (segX + 1); j++) {
    var t = (rt[1] - lt[1]) / segX * j + lt[1];
    var b = (rb[1] - lb[1]) / segX * j + lb[1];
    vertices[(i * (segY + 1) + j) * 2] = (r - l) / segX * j + l;
    vertices[(i * (segY + 1) + j) * 2 + 1] = (b - t) / segY * i + t;
  }
}
this.field.vertices.set(vertices);

that is 4x4 division as 5 points for each edges, the result is shown as screenshot02: https://imgur.com/a/6HREk, but the height of each division is the same, the illusion of perspective is broken 

How can I achieve the result as the screenshot: https://imgur.com/a/ZEUHu 
The height of division proportion to the depth respectively 
Or am I heading to a wrong direction to solve the problem, please suggest me some advise, thanks.

PS. The debug message in the screen is describing the vertices coordinate of the field lay on ground in clipping space, not the screen space.

PIXI and globalCompositeOperation

$
0
0

Hi,

Is it possible to change the globalCompositeOperation in PIXI CANVAS renderer?

I can't find any info about this :(

Thanks for your help,

Fred

for example: globalCompositeOperation = 'destination-out'


Estimating duration of AnimatedSprite's single run

$
0
0

In my game I've a tower sprite (it's a top-down 2D RTS game). I rotate it with my animation mechanism (i.e. I update the sprite.rotation in steps). While rotating I also want to run the firing animation of this tower sprite. To synchronize these two animations I want to know how long (in msec) it will take for one run of the sprite animation.

The only way to specify how fast the AnimatedSprite should run is to set the `speed` property. However this value is an arbitrary scaling factor for internal time. Is there a way to estimate how long a single run of the animation takes for a given `speed` value?

 

Use pixi.js to build a game like agar.io

$
0
0

I want to build a game like agar.io. I create a PIXI.app first and then create a container called clientView insides. I render all my sprites on app.stage and I want to render clientView to my window so that when the client move , I only have to move the container and render it to my windows. Do anyone have good suggestion to implement this, and can I only render a container instead of the whole stage? thx everyone

SVZ8UTt.png?1

Z-order examples outdated?

$
0
0

Hi,

I'm trying to get Layers/Z-order functionality working and following examples on it. There are two different examples on pixijs site

http://pixijs.io/examples/#/layers/zorder.js

http://pixijs.io/examples/#/display/zorder.js

Both have different approaches. But I could not find the functions described there in my pixijs installation.

There's no PIXI.display object, nor is there PIXI.DisplayGroup.

Strange thing is the version I'm running is 4.6.0 (from npm) and the examples above also reportedly running 4.6.0. I also do not find the classes like DisplayGroup in API docs, they are only there in the examples.

What am I missing?

RadialGradient

$
0
0

Hi, I'm new to Pixi, I want to make a star background like this: 

 

But I can't find a way to make 'createRadialGradient' in Pixi, so how to create a star like that?  please help.

 

How to use pixi.js modules/libraries with typescript using npm?

$
0
0

First of all

Is it called "module" or "library" if it extends pixi.js' functionality?

How to use them with Typescript?

I'm not totally new to typescript but aren't pro w/ it at all.
I added pixi.js and the typings via npm (and successfully use npm-modules and their @types in typescript for the server of this project)

But when it comes to pixi.js modules things get weird, it's not enough to add them via npm and adding

import * as Stuff from 'pixi-module'

which makes sense, because afaik they extend the PIXI namespaces. So I tried adding this reference thing

/// <reference path="../node_modules/pixi-module/stuff.d.ts">

but even that did only work half-way. Hard to explain but it was like adding the Apples to PIXI, but not the AppleTree, if I may explain it this way. Even with `require('pixi-module')`

Even stranger: VSCode finds the module and even suggests it when writing an import statement. But after auto-completing it, it tells me the module is not found. (The webpack run tells me so, too.)

Tried it with @ivan.popelyshev pixi-projection, failed half-way and tried it again with pixi-particles. None of them did work as expected in the end.

Wat?

To conclude this: How to add modules after `npm install pixi-module` in my Typescript code, where I did `import * as PIXI from 'pixi.js'`

Thanks <3

Frame Fit Error Override.

$
0
0

Hi,

I'd like to know if there could be any unwanted consequences or behavior if I comment out the error thrown when a frame doesn't fit in the base texture:

https://github.com/pixijs/pixi.js/blob/dev/src/core/textures/Texture.js#L567

The thing is that I'm getting the error between the 8th or 10th decimal, something like this: 20.3333333333 + 20.6666666668 > 41

A brief description of what I'm doing is working with responsive images that are sliced by a specific amount of horizontal slices (depending on the screen height), each slice is an integer. Since the images are scaled before slicing the real size  has to be corrected by that scale which leads to this numbers with all those decimal positions.

Would be even noticeable if the error is commented out?. Could be possible for PIXI to throw an error after creating the slices in another situation?. The slices are moved by the user with touch events and the problem always appears in the final slice and for some specific image sizes and screen resolutions. Unfortunately I don't have any control over the image size.

I don't know how many decimals PIXI actually needs in order to work properly, but this is math versus rendering. The math indicates that the number is bigger, no questions there, but with rendering in mind, will the 8th or 10th decimal of a pixel does even get rendered?. does it make a difference?, is it noticeable even in retina screens?.

extract image array from video

$
0
0

I have an mp4 video file (H264 encoded).

I want to create a PIXI.extras.AnimatedSprite from this video to be able not lagging reversed animation play;

If I splice this video to image sequence the size becoming huge (about 8Mb vs 500kb of mp4).

I want to load low size mp4 file and extract image array from it to create an Animated Sprite.

Reverse playing directly from <video> by changing video.currentTime is lagging. I want to create something like hover/unhover animation.


Pixi Filter/Shader with Confusing Results

$
0
0

I'm experimenting with GLSL shaders in Pixi 4.4, and was trying to make some that would take in two images, the base and an overlay.  The shaders would then replace either the Hue, Saturation, or Value of the pixels in the base image with the H/S/V of the corresponding pixel in the overlay image.  Transparency on the overlay means "no change."

For my tests, I used a 100x100 red square, and the following 100x100 "striped" overlays:

stripesHue.png.0be7f60b48964b955397daf626de4ac5.pngstripesSat.png.a5dca7f672ee175308bf589c741e2373.pngstripesVal.png.d014039e15eeb9729ed1d9e11eac17d6.png

That's the Hue overlay, Saturation overlay, and Value overlay respectively.

Results were only partially consistent, and all wrong.  Here are the results of the Hue and Saturation shaders against a black background.

Screenshot_10.png.b1c030900f3a09b84f40f901e78884f0.pngScreenshot_11.png.f92646dda8f259454134faf1016eafe0.png

Okay, so the stripes are twice as tall as they should be and the bottom-most stripe is outright missing, as though either the overlay or base were resized as some point in the process.  But Value takes the cake:

Screenshot_12.png.c86ad4dd40fb5d0b24f613feb82fb0ca.png

Not only do we have the same problem as above, but there are "teeth" that have appeared off the edge of the 100x100 image (4px in each direction, making a 108x100 image), and there are outlines around every stripe, and if you zoom in especially close you can see that some of the outlines are actually 2 pixels tall, one of near-black, and one of another dark colour, none of which is in the original Value overlay!

I'm at a loss to tell if the problem(s) originates in my shader code or in Pixi, especially since tutorials around the net are mum about how to create a second shader2D uniform in any other setup but Pixi.  I do want to work with Pixi for this project, however, so a fix for Pixi would be appreciated if the problem really is from there.

Here's the HTML/GLSL code.  Please don't mind the If statement, I've already had a few ideas on how to get rid of it:

<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">

<style>
body {
  background-color: black;
  margin: 0;
  overflow: hidden;
}

p {
  color: white;
}
</style>

</head>

<body>

    <script type="text/javascript" src="libs/pixi.js"></script>
	
	<script id="shader" type="shader">
		#ifdef GL_ES
		precision mediump float;
		#endif

		varying vec2 vTextureCoord;
		uniform sampler2D uSampler; //The base image
		uniform sampler2D overlay; //The overlay

		vec3 rgb2hsv(vec3 c)
		{
			vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
			vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
			vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));

			float d = q.x - min(q.w, q.y);
			float e = 1.0e-10;
			return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
		}

		vec3 hsv2rgb(vec3 c)
		{
			vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
			vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
			return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
		}

		void main(void) {
			vec4 baseRGB = texture2D(uSampler, vTextureCoord);
			vec4 overlayRGB = texture2D(overlay, vTextureCoord);
			
			if(overlayRGB.a > 0.0) {
				vec3 baseHSV = rgb2hsv(baseRGB.rgb);
				vec3 overlayHSV = rgb2hsv(overlayRGB.rgb);

				// Hue
				// vec3 resultHSV = vec3(overlayHSV.x, baseHSV.y, baseHSV.z);

				// Saturation
				// vec3 resultHSV = vec3(baseHSV.x, overlayHSV.y, baseHSV.z);

				// Value
				vec3 resultHSV = vec3(baseHSV.x, baseHSV.y, overlayHSV.z);

				vec3 resultRGB = hsv2rgb(resultHSV);
			
				gl_FragColor = vec4(resultRGB.rgb, baseRGB.a);
			}
			else {
				gl_FragColor = baseRGB;
			}
		}
		
	</script>
	
	<script type="text/javascript" src="replaceTest.js"></script>
	
</body>
</html>

 

And here's the JS:

var width = window.innerWidth;
var height = window.innerHeight;
var renderer = new PIXI.WebGLRenderer(width, height);
document.body.appendChild(renderer.view);

var stage = new PIXI.Container();

var sprite = PIXI.Sprite.fromImage('flat.png');
sprite.x = width / 2;//Set it at the center of the screen
sprite.y = height / 2;
sprite.anchor.set(0.5);//Make sure the center point of the image is at its center, instead of the default top left
stage.addChild(sprite);

//Create a uniforms object to send to the shader
var uniforms = {}

uniforms.overlay = {
	type:'sampler2D',
	value: PIXI.Texture.fromImage('stripesVal.png') // or stripesSat, stripesHue, etc
}

//Get shader code as a string
var shaderCode = document.getElementById("shader").innerHTML;

//Create our Pixi filter using our custom shader code
var rasShader = new PIXI.Filter(null,shaderCode,uniforms);
console.log(rasShader.uniforms);
sprite.filters = [rasShader];


function update() {
    requestAnimationFrame(update);
    renderer.render(stage);
}
update();

Any help would be appreciated!

Very slow CanvasRenderer performance for just 3 sprites

$
0
0

I'm trying to draw 3 'tetris blocks' on the screen, but as you can see, the performance is quite bad. I get around 3 frames per second on my machine.

I created a codepen example to demonstrate the effect.

What I do is create one Base texture (i.e. a tilemap) and then create 3 sprites from that base texture, and adding it to the stage. That should be fast, right?

Is there anything obvious that I'm missing?

Thanks!

Rendering Only "In Screen" objects

$
0
0

Hello,

I'm doing some stress test with PIXI, and it seems that PIXI draw all the element of the stage, it does not matter if they are in range off the screen or not.

I'm trying with 300 circles and even if I move the layer to coordinates very far from the "screen view" (x=10000,y=10000), it still processing them all and give me very low FPS

There is any way to process only "inrange" objects to save CPU/GPU usage?

It is a Normal Draw (drawCircle) slower or heavier than an Image?

Thanks

PIXI 4.6+ way of cutting shapes using other shapes?

$
0
0

I've been reading a backlog of how to do this all the way back to when the recommendation was to use canvas. What is the new and modern way of accomplishing this? Let's say I have a rectangle that fills my screen and makes the game look like night time.. then I have a torch and I want to cut (or lighten) a circle out of the night rectangle.

I've tried drawing a circle using graphics and setting its colors to white, black, and alphas to 1.0 or 0, then adding it as a mask onto the rectangle -- this only seems to perform a regular mask operation (aka draw the night within the circle) but never the opposite (draw the nighttime everywhere but the circle). I know how to accomplish this via canvas or webgl, but it looks like this feature is already in pixi I'm just not sure how to do it!

Thanks for reading!

p.s. if the answer involves using a filter could you please reply with code.. im never sure which object exactly gets the filter added

EDIT: and will the new method work if calculated *every frame* ?

How to implement accurate sprite scaling

$
0
0

Hello,

First of all, I know that the problem has been somewhat addressed in previous topics, but I just don't seem to find an answer which results to as accurate zoom effect what I am looking for.

My use case is that I want to implement scaling a sprite (many of them in a container) which is as accurate as scaling pixi graphics object. -- which does not get blurred.

The scale range goes e.g. from 1.0 -> 10.0

I tried solve the problem by creating a tileset which has the same sprite stored in multiple sizes 64x64, 128x128 and when I am scaling I swap the texture of the sprite according to current scale level. e.g.

The code below illustrates the idea:
 

...

var twitter = new PIXI.Sprite(tileset['twitter-zoom-1.png']);
twitter.x = 100;
twitter.y = 100;
twitter.currentTexture = 'zoom-1';
twitter.anchor.set(0.5, 0.5);

this.addChild(twitter); // Add to stage

// Simple test...
setInterval(function() {
  twitter.scale.x += 0.1;
  twitter.scale.y += 0.1;

  // Swap the texture when reaching 2.0 ... (but only if not swapped)
  if(twitter.scale.x >= 2.0 && twitter.currentTexture === 'zoom-1') {
    twitter.currentTexture = 'zoom-2';
    twitter.texture = tileset['twitter-zoom-2.png'];
    twitter.scale.set(1.0, 1.0); // Reset scale
  }
}, 1);

...

I am just not happy with this approach for multiple reasons:

1.) The check I use for scale is never really accurate because of floating-point numbers

2.) I need to save up to 10 different zoom levels per sprite to the tileset

3.) When the sprite is zoomed from e.g. 1.0 - 1.9 I can see it getting blurry and then suddenly it gets a bit more accurate again because the sprite is swapped.

Also I don't know if this is even right approach for solving the problem in terms of performance.

Finally, I have the .svg version of the twitter icon which opens more opportunities.

Any ideas? Cheers! :)

 

Viewing all 3978 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>