FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

charmy dance example · jli/processing@3edfa2e · GitHub

This repository was archived by the owner on May 28, 2020. It is now read-only.

Repository navigation

Commit 3edfa2e

Browse files
committed
charmy dance example
1 parent f0e4b8e commit 3edfa2e

6 files changed

Lines changed: 252 additions & 0 deletions

File tree

‎app/src/processing/mode/javascript/DirectivesEditor.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ void findRemoveDirectives ( boolean clean )
143143
{
144144
String mm = m.group();
145145

146+
// TODO this urgently needs tests ..
147+
146148
/* remove framing */
147149
mm = mm.replaceAll("^\\/\\*\\s*@pjs","").replaceAll("\\s*\\*\\/\\s*$","");
148150
/* fix multiline nice formatting */
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* @pjs transparent=true; */
2+
3+
/**
4+
* <p>This example shows you how to use video with a transparency
5+
* which currently is not natively supported by web video formats (H264 / OGV).</p>
6+
*
7+
* <p>The "disco effects" in the back are generated by Processing code
8+
* while Charmy dancing upfront is coming from a HTML5 video element.</p>
9+
*
10+
* <p>The video contains both the image and the mask side by side, have a look
11+
* at the war file here: <a href="charmy.mp4">charmy.mp4</a></p>
12+
*
13+
* Greenscreen material by <a href="http://mint.gs/">mint.gs</a> and used with permission.
14+
*
15+
* <video width="480" height="360" style="display:none">
16+
* <source src="charmy.mp4" />
17+
* <source src="charmy.ogv" />
18+
* </video>
19+
*/
20+
21+
Video video;
22+
PImage frame;
23+
float fSec = 1/25.0;
24+
int fMillis = int(1000 * fSec);
25+
int ts = 0;
26+
27+
void setup ()
28+
{
29+
size( 320, 240 );
30+
31+
noStroke();
32+
textFont(createFont("Arial", 20));
33+
background(255,0);
34+
}
35+
36+
void draw ()
37+
{
38+
if ( video != null )
39+
{
40+
background(255, 0);
41+
42+
timeLineEffects();
43+
44+
PImage frame = getFrame(video);
45+
PImage rgbImage = frame.get(frame.width/2,0,frame.width/2,frame.height);
46+
PImage maskImage = frame.get(0,0,frame.width/2,frame.height);
47+
rgbImage.mask(maskImage);
48+
image( rgbImage, width/2-rgbImage.width/2, 0 );
49+
}
50+
}
51+
52+
void mousePressed ()
53+
{
54+
if ( video != null )
55+
{
56+
if ( video.paused ) video.play();
57+
else video.pause();
58+
}
59+
}
60+
61+
/* called from JavaScript to set the freshly loaded video */
62+
void addVideo ( Video v )
63+
{
64+
v.play();
65+
video = v;
66+
}
67+
68+
/* copy video image to PImage */
69+
PImage getFrame ( Video v )
70+
{
71+
return new PImage(v); // sub-optimal ..
72+
}
73+
74+
/* make Processing understand the HTMLVideoElement */
75+
interface Video
76+
{
77+
boolean autoplay;
78+
boolean controls;
79+
80+
int width;
81+
int height;
82+
int videoWidth; /*readonly*/
83+
int videoHeight; /*readonly*/
84+
85+
boolean muted;
86+
float volume;
87+
88+
boolean loop;
89+
boolean paused;
90+
boolean ended;
91+
92+
String currentSrc;
93+
94+
float duration;
95+
float currentTime;
96+
97+
void play();
98+
void pause();
99+
}
615 KB
Binary file not shown.
620 KB
Binary file not shown.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
// wait for the DOM to become ready
3+
window.onload = function () {
4+
5+
// try to find the Processing sketch instance, or retry
6+
function tryFindSketch () {
7+
var sketch = Processing.instances[0];
8+
if ( sketch == undefined )
9+
setTimeout(tryFindSketch, 200); // retry in 0.2 secs
10+
else
11+
initVideos( sketch );
12+
}
13+
tryFindSketch();
14+
}
15+
16+
// look for the video and send to sketch
17+
function initVideos ( sketch ) {
18+
19+
var video = document.getElementsByTagName("video")[0];
20+
21+
video['loop'] = true; // as "loop" is not supported by many browsers
22+
23+
// similar to tryFindSketch this creates a loop that
24+
// continues until the video becomes ready.
25+
(function( s, v ){
26+
var tryAddVideo = function () {
27+
if ( v.readyState > 0 ) {
28+
s.addVideo(v);
29+
} else {
30+
setTimeout(tryAddVideo, 200);
31+
}
32+
}
33+
tryAddVideo();
34+
})( sketch, video );
35+
36+
// force loop when set as this is currently not supported by most browsers
37+
addEvent(video,'ended',function(){
38+
if ( 'loop' in this && this.loop ) {
39+
this.currentTime = 0;
40+
this.play();
41+
}
42+
});
43+
}
44+
45+
// http://html5demos.com/
46+
var addEvent = (function () {
47+
if (document.addEventListener) {
48+
return function (el, type, fn) {
49+
if (el && el.nodeName || el === window) {
50+
el.addEventListener(type, fn, false);
51+
} else if (el && el.length) {
52+
for (var i = 0; i < el.length; i++) {
53+
addEvent(el[i], type, fn);
54+
}
55+
}
56+
};
57+
} else {
58+
return function (el, type, fn) {
59+
if (el && el.nodeName || el === window) {
60+
el.attachEvent('on' + type, function () { return fn.call(el, window.event); });
61+
} else if (el && el.length) {
62+
for (var i = 0; i < el.length; i++) {
63+
addEvent(el[i], type, fn);
64+
}
65+
}
66+
};
67+
}
68+
})();
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
void timeLineEffects ()
2+
{
3+
float t = video.duration/10;
4+
float n = video.currentTime;
5+
6+
fill( 255 );
7+
8+
if ( n < t )
9+
{
10+
float w = map(n,0,t,100,500);
11+
ellipse( width/2,height/2,w,w);
12+
}
13+
else if ( n < 2*t )
14+
{
15+
float w = map(n,t,2*t,0,300);
16+
17+
fill(255);
18+
ellipse( width/2-100,height/2-100,w*0.75,w*0.75);
19+
fill(0);
20+
ellipse( width/2+50,height/2,w,w);
21+
fill(255);
22+
ellipse( width/2-100,height/2+150,w*1.5,w*1.5);
23+
}
24+
else if ( n < 3*t )
25+
{
26+
float w = map(n,2*t,3*t,0,1);
27+
28+
for ( int i = 0 ; i < 9; i++ )
29+
{
30+
rect((i%3)*width/3,int(i/3)*height/3,width/3*w,height/3*w);
31+
}
32+
}
33+
else if ( n < 4*t )
34+
{
35+
float w = map(n,3*t,4*t,-1,0);
36+
37+
fill( 0 );
38+
for ( int i = 0 ; i < 25; i++ )
39+
{
40+
rect(((i%5)+1)*width/5,(int(i/5)+1)*height/5,width/5*w,height/5*w);
41+
}
42+
}
43+
else if ( n < 5*t )
44+
{
45+
float w = map(n,4*t,5*t,0,height/9);
46+
47+
for ( int i = 0 ; i < 10; i++ )
48+
{
49+
rect(0,i*(height/9)-w, width, height/18);
50+
}
51+
}
52+
else if ( n < 7*t )
53+
{
54+
float w = map(n,6*t,7*t,0,TWO_PI);
55+
for ( int i = 0 ; i < 22; i++ )
56+
{
57+
fill( cos(w) > 0 ? 255 : 0 );
58+
rect(i*(width/20), 0, width/40, height);
59+
}
60+
}
61+
else if ( n < 8*t )
62+
{
63+
int mx = int(map(n,t*7,t*8,1,20));
64+
65+
for ( int i = 0; i < mx; i++ )
66+
ellipse( random(width), random(height), 5*mx, 5*mx );
67+
}
68+
else if ( n < 9*t )
69+
{
70+
int mxx = 1+int(map(n,t*8,t*9,1,10))*2;
71+
mx = mxx*mxx;
72+
73+
for ( int i = 0 ; i < mx; i+=2 )
74+
{
75+
rect( (i%mxx)*width/mxx, int(i/mxx)*height/mxx, width/mxx, height/mxx );
76+
}
77+
}
78+
else if ( n < 10*t )
79+
{
80+
float w = map(n,9*t,10*t,700,100);
81+
ellipse( width/2,height/2,w,w);
82+
}
83+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL