This repository has been archived by the owner on Nov 10, 2020. It is now read-only.
forked from mozilla/popcorn-js
-
Notifications
You must be signed in to change notification settings - Fork 6
/
popcorn.processing.js
127 lines (112 loc) · 3.71 KB
/
popcorn.processing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// PLUGIN: Processing
/**
* Processing popcorn plug-in
*/
(function (Popcorn) {
var processingLoaded = false,
toggle = function( on, options ) {
var pcorn = options.popcornInstance,
instance = options.pjsInstance,
canvas = options.canvas;
if ( canvas && options.isReady ) {
if ( on ) {
canvas.style.display = "inline";
!pcorn.media.paused && instance.loop();
} else {
canvas.style.display = "none";
instance.noLoop();
}
} else {
setTimeout ( function() {
toggle ( on, options );
}, 10 );
}
},
load = function() {
Popcorn.getScript( "http://processingjs.org/content/download/processing-js-1.2.1/processing-1.2.1.js", function() {
processingLoaded = true;
});
};
load();
Popcorn.plugin( "processing" , function ( options ) {
var init = function( context ) {
var popcorn = context,
initProcessing,
parentTarget = document.getElementById( options.target );
if ( parentTarget.tagName === "CANVAS" ) {
options.canvas = parentTarget;
} else if ( parentTarget.tagName === "DIV" ) {
options.canvas = document.createElement( "canvas" );
// +new Date() is used here to create unique id's for canvas' within the same div.
options.canvas.id = options.target + "Sketch" + +new Date();
options.canvas.setAttribute( "data-processing-sources", options.sketch );
parentTarget.appendChild( options.canvas );
}
options.canvas.style.display = "none";
Popcorn.xhr({
url: options.sketch,
dataType: "text",
success: function( responseCode ) {
codeReady = true;
processingCode = responseCode;
initProcessing();
}
});
initProcessing = function() {
if ( codeReady && window.Processing ) {
options.pjsInstance = new Processing( options.canvas, processingCode );
popcorn.listen( "pause", function () {
if ( options.canvas.style.display === "inline" ) {
options.pjsInstance.noLoop();
}
} );
popcorn.listen( "play", function() {
if ( options.canvas.style.display === "inline" ) {
options.pjsInstance.loop();
}
});
options.isReady = true;
} else {
setTimeout ( initProcessing, 10 );
}
};
};
return {
manifest: {
about: {
name: "Popcorn Processing Plugin",
version: "0.1",
author: "Christopher De Cairos, Benjamin Chalovich",
website: "cadecairos.blogspot.com, ben1amin.wordpress.org"
},
options: {
start : { elem: "input", type: "text", label: "In" },
end : { elem: "input", type: "text", label: "Out" },
control : { elem: "input", type: "text", label: "Control" },
target : { elem: "input", type: "text", label: "Target" },
sketch : { elem: "input", type: "text", label: "Sketch" },
func : { elem: "input", type: "text", label: "Function" }
}
},
_setup: function( options ) {
var processingCode,
codeReady = false,
self = this;
readyCheck = function() {
if ( !processingLoaded ) {
load();
}
},
readyCheck();
init( self );
options.popcornInstance = this;
},
start: function( event, options ) {
toggle ( true, options );
},
end: function( event, options ) {
toggle ( false, options );
}
};
});
}( Popcorn ));