This application uses Canvas, Animation Functions, XSJS services to Controlling the drawing of a squares.
Information will be saved/retrieved from HANA DB, based on the current positions of the squares.
The BLUE square in the demo is controlled by the information stored in configuration tables in HANA.
Demo Video:
http://www.youtube.com/watch?v=oZou6tMIb14
.INDEX.HTML: Code to draw the TRACK
.VIEW.JS : Code to draw the squares and its movement
.CONTROLLER.JS: Calling methods
Draw a TRACK for defining moving path for the squares.
Alternately you can upload a picture of the TRACK using commands like
INDEX.HTML
<canvas id="myCanvas" width="1500" height="400"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
var width = 1000;
var height = 150;
var x = 140;
var y = 100;
var radius = 50;
var color = "rgb(196, 171, 254)";
</script>
<script>
var view = sap.ui.view({id:"idrace1", viewName:"race.race", type:sap.ui.core.mvc.ViewType.JS});
</script>
.VIEW.JS
Code in CreateContent : function(oController)
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {
};
})();
// square one
function drawRectangle(myRectangle, context) {
}
function animate(myRectangle, canvas, context, startTime) {
// the below numbers are the position coordinates - x-axis & y-axis of the squares
// these numbers can be dynamically picked from HANA DB
// the below condition defines the speed, direction, and postion of the square.
// applied same logic to control the square movement, few more if statements
// repeated the same logic for SQUARE 2
if (myRectangle.x >= 145 & myRectangle.x <=1116 & myRectangle.y >= 100 & myRectangle.y <= 233 ) {
var spd = oResult.getText();
$(function(oEvent){
if (spd == 'null' || spd==null || spd === 'null' || spd === null ||spd.length === 0 || typeof spd === "undefined")
{
var Movobj = 'SQ1';
} //if
}); //end of func
…
…
….
// requesting frame
requestAnimFrame(function() {
animate2(myRectangle2, canvas, context, startTime);
});
} //end
//Drawing squares
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var myRectangle = {
x: 145,
y: 100,
width: 12,
height: 12,
borderWidth: 1
};
drawRectangle(myRectangle, context);
var myRectangle2 = {
x: 400,
y: 100,
width: 12,
height: 12,
borderWidth: 1
};
drawRectangle2(myRectangle2, context);
// wait one second before starting animation
setTimeout(function() {
var startTime = (new Date()).getTime();
animate(myRectangle, canvas, context, startTime);
}, 1000);
// wait one second before starting animation
setTimeout(function() {
var startTime = (new Date()).getTime();
animate2(myRectangle2, canvas, context, startTime);
}, 1000);
.CONTROLLER.JS
Methods are used
Sending X,Y Co-ordinates Information of both the squares.
var aUrl = '../../../session/services/MultiplyTest.xsjs?cmd=speed'+'&x1='+x1+'&y1='+y1+'&x2='+x2+'&y2='+y2;
jQuery.ajax({
url: aUrl,
method: 'GET',
dataType: 'json',
success: this.onCompleteSpeed,
error: this.onErrorCall });
},
var aUrl = '../../../session/services/MultiplyTest.xsjs?cmd=race1'+'&Movobj='+Movobj;
jQuery.ajax({
url: aUrl,
method: 'GET',
dataType: 'json',
success: this.onCompleteMultiply,
error: this.onErrorCall });
},
.XSJS
Below are the sample codes to retrieve the data by
function performSpeed(){
var body = '';
var x1 = $.request.parameters.get('x1');
var y1 = $.request.parameters.get('y1');
var x2 = $.request.parameters.get('x2');
var y2 = $.request.parameters.get('y2');
try {
var query = 'CALL "amohas97.session.models::get_speed"(?,?,?,?,? )';
$.trace.debug(query);
var conn = $.db.getConnection();
var cstmt = conn.prepareCall(query);
body += cstmt.getDecimal(5) + "\n";
} catch (e) {
$.response.status = $.net.http.INTERNAL_SERVER_ERROR;
$.response.setBody(e.message);
return;
}
$.response.setBody(body);
$.response.status = $.net.http.OK;
}
function performRace1(){
var body = '';
var Movobj = $.request.parameters.get('Movobj');
try {
var query =
'select "Defaults" FROM "WRK_SCH"."amohas97.session.data::sdyn" WHERE "Movobj" = ? ';
$.trace.debug(query);
var conn = $.db.getConnection();
var pstmt;
var rs;
pstmt = conn.prepareStatement(query);
rs = pstmt.executeQuery();
while (rs.next()) {
body += rs.getNString(1) + "\n";
//var answer = '0';
//body = answer.toString();
}
} catch (e) {
$.response.status = $.net.http.INTERNAL_SERVER_ERROR;
$.response.setBody(e.message);
return;
}
//num2 - num1;
$.response.setBody(body);
$.response.status = $.net.http.OK;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
25 | |
16 | |
14 | |
12 | |
10 | |
9 | |
9 | |
8 | |
8 | |
8 |