Skip to content

Instantly share code, notes, and snippets.

@profOnno
Created December 17, 2016 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save profOnno/43871c166115540ee8b843a0f769f534 to your computer and use it in GitHub Desktop.
Save profOnno/43871c166115540ee8b843a0f769f534 to your computer and use it in GitHub Desktop.
OpenJSCAD clock animation
//
// clock animation
// for fast animation three.js or babylon.js ?..
// timeout bug... <shift><enter> starts new worker? old timer keeps running
function clock(hour, minute, second) {
var t = second + 60 * (minute + 60 * hour);
var h = cylinder({r:0.3,h:4}).setColor(1,0,0).rotateX(-t/60/60/12*360);
var m = cylinder({r:0.2,h:5}).setColor(0,1,0).rotateX(-t/60/60*360);
var s = cylinder({r:0.1,h:6}).setColor(0,0,1).rotateX(-t/60*360);
var c = cylinder({r:9}).setColor(0.7,1,0.7,0.5).rotateY(90).translate([-1,0,0]);
return union(c, h, m, s).rotateY(-90).translate([0,0,5]);
}
function loop() {
var d = new Date();
self.postMessage({cmd:'rendered',
objects: [
makeTime().toCompactBinary(),
clock(d.getHours(), d.getMinutes(), d.getSeconds()).toCompactBinary()
]});
// setTimeout(function () {loop();}, 100);
}
var digits = {}; // hold number objects;
function digitsInit(argO) {
var l;
for (var i=0;i<10;i++) {
var o=[];
l = vector_text(0,0,""+i); // l contains a list of polylines to be drawn
for (var ii=0; ii<l.length; ii++) {
o.push(rectangular_extrude(l[ii], {w: argO.width/argO.scale, h: argO.height/argO.scale})); // extrude it to 3D
}
digits[i]=union(o).rotateZ(90).scale(argO.scale);
}
// add colon
o=[];
l = vector_text(0,0,":"); // l contains a list of polylines to be drawn
for (ii=0; ii<l.length; ii++) {
o.push(rectangular_extrude(l[ii], {w: argO.width/argO.scale, h: argO.height/argO.scale})); // extrude it to 3D
}
digits[":"]=union(o).rotateZ(90).scale(argO.scale);
}
function makeTime(indate){
var _date = new Date(),
str = [],
tmp,
timeO = [];
// innerfunction, outside faster???
var doPush = function (arr, _str) {
if (_str.length < 2) {
arr.push({s:"not set", d:digits["0"]});
arr.push({s:_str[0], d:digits[_str[0]]});
} else {
arr.push({s:_str[0], d:digits[_str[0]]});
arr.push({s:_str[1], d:digits[_str[1]]});
}
}
doPush(str, _date.getHours().toString());
str.push({d:digits[":"],div:true});
doPush(str, _date.getMinutes().toString());
str.push({d:digits[":"],div:true});
doPush(str, (0+_date.getSeconds()).toString())
var a=0,
w=0;
var ostr = str.map(function(dat){
a = a+w;
w = dat.div?2:4;
return dat.d.translate([0,a,0]);
})
// don't do the scale and rotate in the loop..
// not tested if they use a lot of cpu though
str = union(ostr).translate([-8,-14.5,0]);
return str;
}
function main() {
digitsInit({width:0.8, height: 8, scale:0.2});
//loop(); // use this when using setTimeout int the loop
setInterval(loop,1000); // setInterval can be bad on slow pc
return cube(0.01) // return "pixel" to avoid warnings
}
@profOnno
Copy link
Author

Click show me to view this code on OpenJSCAD

@jumpjack
Copy link

How do I define vector_text?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment