Move some logic (with clipRect) from script to NodeJS code