Subversion Repositories wimsdev

Rev

Rev 7748 | Rev 7779 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7748 Rev 7749
Line 180... Line 180...
180
        type = get_token(infile);
180
        type = get_token(infile);
181
        done = FALSE;
181
        done = FALSE;
182
        /*
182
        /*
183
        @canvasdraw
183
        @canvasdraw
184
        @will try use the same syntax as flydraw or svgdraw to paint a html5 bitmap image<br />by generating a tailor-made javascript include file: providing only the js-functionality needed to perform the job.<br />thus ensuring a minimal strain on the client browser <br />(unlike some popular 'canvas-do-it-all' libraries, who have proven to be not suitable for low-end computers found in schools...)
184
        @will try use the same syntax as flydraw or svgdraw to paint a html5 bitmap image<br />by generating a tailor-made javascript include file: providing only the js-functionality needed to perform the job.<br />thus ensuring a minimal strain on the client browser <br />(unlike some popular 'canvas-do-it-all' libraries, who have proven to be not suitable for low-end computers found in schools...)
185
        @General syntax <ul><li>The transparency of all objects can be controlled by command 'opacity [0-255],[0,255]'</il><li>a line based object can be controlled by command 'linewidth int'</li><li>a line based object may be dashed by using keyword 'dashed' before the object command.<br />the dashing type can be controled by command 'dashtype int,int'</li><li>a fillable object can be set fillable by starting the object command with an 'f'<br />(like frect,fcircle,ftriangle...)<br />or by using the keyword 'filled' before the object command.<br />The fill colour will be the stroke colour...(19/10/2013)<li> a draggable object can be set draggable by a preceding command 'drag x/y/xy'<br />The translation can be read by javascript:read_dragdrop();<br />Multiple objects may be set draggable / clickable (no limit)<br /> not all flydraw objects may be dragged / clicked<br />Only draggable / clickable objects will be scaled on zoom and will be translated in case of panning</li><li> a 'onclick object' can be set 'clickable' by the preceding keyword 'onclick'<br />not all flydraw objects can be set clickable</li><li><b>remarks using a ';' as command separator</b><br />commands with only numeric or colour arguments may be using a ';' as command separator (in stead of a new line)<br />commands with a string argument may not use a ';' as command separator !<br />these exceptions are not really straight forward... so keep this in mind.<br />example:<br />size 200,200;xrange -5,5;yrange -5,5;hline 0,0,black;vline 0,0,black<br />plot red,sin(x)<br />drag xy<br />html 0,0,5,-5, &euro; <br />lines green,2,0,2,-2,-2,2,-2,0;rectangle 1,1,4,4,purple;frectangle -1,-1,-4,-4,yellow</li></ul>
185
        @General syntax <ul><li>The transparency of all objects can be controlled by command 'opacity [0-255],[0,255]'</il><li>a line based object can be controlled by command 'linewidth int'</li><li>a line based object may be dashed by using keyword 'dashed' before the object command.<br />the dashing type can be controled by command 'dashtype int,int'</li><li>a fillable object can be set fillable by starting the object command with an 'f'<br />(like frect,fcircle,ftriangle...)<br />or by using the keyword 'filled' before the object command.<br />The fill colour will be the stroke colour...(19/10/2013)<li> a draggable object can be set draggable by a preceding command 'drag x/y/xy'<br />The translation can be read by javascript:read_dragdrop();<br />Multiple objects may be set draggable / clickable (no limit)<br /> not all flydraw objects may be dragged / clicked<br />Only draggable / clickable objects will be scaled on zoom and will be translated in case of panning</li><li> a 'onclick object' can be set 'clickable' by the preceding keyword 'onclick'<br />not all flydraw objects can be set clickable</li><li><b>remarks using a ';' as command separator</b><br />commands with only numeric or colour arguments may be using a ';' as command separator (in stead of a new line)<br />commands with a string argument may not use a ';' as command separator !<br />these exceptions are not really straight forward... so keep this in mind.<br />example:<br />size 200,200;xrange -5,5;yrange -5,5;hline 0,0,black;vline 0,0,black<br />plot red,sin(x)<br />drag xy<br />html 0,0,5,-5, &amp;euro; <br />lines green,2,0,2,-2,-2,2,-2,0;rectangle 1,1,4,4,purple;frectangle -1,-1,-4,-4,yellow</li></ul>
186
        */
186
        */
187
        switch(type){
187
        switch(type){
188
        case END:
188
        case END:
189
        finished = 1;
189
        finished = 1;
190
        done = TRUE;
190
        done = TRUE;
Line 891... Line 891...
891
        @ if set two textareas are added to the document<br />(one for x-values , one for y-values)
891
        @ if set two textareas are added to the document<br />(one for x-values , one for y-values)
892
        @ the student may use this as correction for (x:y) on a drawing (or to draw without mouse, using just the coordinates)
892
        @ the student may use this as correction for (x:y) on a drawing (or to draw without mouse, using just the coordinates)
893
        @ can not be combined with command "intooltip tiptext" <br />note: the 'tooltip div element' is used for placing inputfields
893
        @ can not be combined with command "intooltip tiptext" <br />note: the 'tooltip div element' is used for placing inputfields
894
        @ user drawings will not zoom on zooming (or pan on panning)
894
        @ user drawings will not zoom on zooming (or pan on panning)
895
        */
895
        */
896
        if(use_tooltip == TRUE){canvas_error("userinput_xy can not be combined with intooltip command");}
896
            if(use_tooltip == TRUE){canvas_error("usertextarea_xy can not be combined with intooltip command");}
-
 
897
            if( use_input_xy == 1 ){canvas_error("usertextarea_xy can not be combined with userinput_xy command");}
897
        use_input_xy = 2;
898
            fprintf(js_include_file,"function safe_eval(exp){if(exp.indexOf('^') != -1){exp = exp.replace(/\\s*(.*)\\^\\s*(.*)/ig, \"pow($1, $2)\");};var reg = /(?:[a-z$_][a-z0-9$_]*)|(?:[;={}\\[\\]\"'!&<>^\\\\?:])/ig;var valid = true;exp = exp.replace(reg,function($0){if (Math.hasOwnProperty($0)){return \"Math.\"+$0;}else{valid = false;};});if (!valid){alert(\"hmmm \"+exp+\" ?\"); exp = null;}else{try { exp = eval(exp); } catch (e) {alert(\"Invalid arithmetic expression\"); exp = null;};};return exp;};");
-
 
899
            use_input_xy = 2;
898
        break;
900
            break;
899
        case USERINPUT_XY:
901
        case USERINPUT_XY:
900
        /*
902
        /*
901
        @ userinput_xy
903
        @ userinput_xy
902
        @ keyword
904
        @ keyword
903
        @ to be used in combination with command "userdraw object_type,color"
905
        @ to be used in combination with command "userdraw object_type,color"
904
        @ if set two (or three) input fields are added to the document<br />(one for x-values , one for y-values and in case of drawing circle one for radius-values)
906
        @ if set two (or three) input fields are added to the document<br />(one for x-values , one for y-values and in case of drawing circle one for radius-values)
905
        @ the student may use this as correction for (x:y) on a drawing (or to draw without mouse, using just the coordinates)
907
        @ the student may use this as correction for (x:y) on a drawing (or to draw without mouse, using just the coordinates)
-
 
908
        @ math input is allowed (e.g something like: 1+3,2*6,1/3,sqrt(3), sin(pi/4),10^-2,log(2)...)<br />eval function is 'protected' against code injection.
906
        @ can not be combined with command "intooltip tiptext" <br />note: the 'tooltip div element' is used for placing inputfields
909
        @ can not be combined with command "intooltip tiptext" <br />note: the 'tooltip div element' is used for placing inputfields
907
        @ user drawings will not zoom on zooming (or pan on panning)
910
        @ user drawings will not zoom on zooming (or pan on panning)
908
        */
911
        */
909
        if(use_tooltip == TRUE){canvas_error("userinput_xy can not be combined with intooltip command");}
912
            if(use_tooltip == TRUE){canvas_error("userinput_xy can not be combined with intooltip command");}
-
 
913
            /* add simple eval check to avoid code injection with unprotected eval(string) */
910
        use_input_xy = 1;
914
            fprintf(js_include_file,"function safe_eval(exp){if(exp.indexOf('^') != -1){exp = exp.replace(/\\s*(.*)\\^\\s*(.*)/ig, \"pow($1, $2)\");};var reg = /(?:[a-z$_][a-z0-9$_]*)|(?:[;={}\\[\\]\"'!&<>^\\\\?:])/ig;var valid = true;exp = exp.replace(reg,function($0){if (Math.hasOwnProperty($0)){return \"Math.\"+$0;}else{valid = false;};});if (!valid){alert(\"hmmm \"+exp+\" ?\"); exp = null;}else{try { exp = eval(exp); } catch (e) {alert(\"Invalid arithmetic expression\"); exp = null;};};return exp;};");
-
 
915
            use_input_xy = 1;
911
        break;
916
            break;
912
        case USERDRAW:
917
        case USERDRAW:
913
        /*
918
        /*
914
        @ userdraw object_type,color
919
        @ userdraw object_type,color
915
        @ implemented object_type: <ul><li>point</li><li>points</li><li>crosshair</li><li>crosshairs</li><li>line</li><li>lines</li><li>segment</li><li>segments</li><li>polyline</li><li>circle</li><li>circles</li><li>arrow</li><li>arrows</li><li>triangle</li><li>polygon</li><li>poly[3-9]</li><li>rect</li><li>roundrect</li><li>rects</li><li>roundrects</li><li>freehandline</li><li>freehandlines</li><li>path</li><li>paths</li><li>text</li></ul>
920
        @ implemented object_type: <ul><li>point</li><li>points</li><li>crosshair</li><li>crosshairs</li><li>line</li><li>lines</li><li>segment</li><li>segments</li><li>polyline</li><li>circle</li><li>circles</li><li>arrow</li><li>arrows</li><li>triangle</li><li>polygon</li><li>poly[3-9]</li><li>rect</li><li>roundrect</li><li>rects</li><li>roundrects</li><li>freehandline</li><li>freehandlines</li><li>path</li><li>paths</li><li>text</li></ul>
916
        @ note: mouselisteners are only active if "$status != done " (eg only drawing in an active/non-finished exercise) <br /> to overrule use command/keyword "status" (no arguments required)
921
        @ note: mouselisteners are only active if "$status != done " (eg only drawing in an active/non-finished exercise) <br /> to overrule use command/keyword "status" (no arguments required)
Line 5720... Line 5725...
5720
   };\
5725
   };\
5721
  };\
5726
  };\
5722
 };\n\
5727
 };\n\
5723
 var stepy = Math.abs(y2px(ymajor) - y2px(0));\
5728
 var stepy = Math.abs(y2px(ymajor) - y2px(0));\
5724
 var minor_step = stepy / yminor;\
5729
 var minor_step = stepy / yminor;\
5725
 for(var y = 0 ; y < ysize - xmarge ; y = y + stepy){\
5730
 for(var y = 0 ; y < ysize - stepy ; y = y + stepy){\
5726
  ctx.strokeStyle=\"rgba(\"+major_color+\",\"+major_opacity+\")\";\
5731
  ctx.strokeStyle=\"rgba(\"+major_color+\",\"+major_opacity+\")\";\
5727
  ctx.lineWidth = line_width;\n\
5732
  ctx.lineWidth = line_width;\n\
5728
  ctx.beginPath();\n\
5733
  ctx.beginPath();\n\
5729
  ctx.moveTo(xmarge,y);\n\
5734
  ctx.moveTo(xmarge,y);\n\
5730
  ctx.lineTo(xsize,y);\n\
5735
  ctx.lineTo(xsize,y);\n\
Line 5819... Line 5824...
5819
 };\n\
5824
 };\n\
5820
 var stepx = Math.abs(x2px(xmajor) - x2px(0));\
5825
 var stepx = Math.abs(x2px(xmajor) - x2px(0));\
5821
 var minor_step = stepx / xminor;\
5826
 var minor_step = stepx / xminor;\
5822
 var prec = Math.log(precision)/Math.log(10);\
5827
 var prec = Math.log(precision)/Math.log(10);\
5823
 var xtxt;var corr;var flip = 0;\
5828
 var xtxt;var corr;var flip = 0;\
5824
 for(var x = xmarge ; x < xsize ; x = x + stepx){\
5829
 for(var x = stepx ; x < xsize ; x = x + stepx){\
5825
  ctx.strokeStyle=\"rgba(\"+major_color+\",\"+major_opacity+\")\";\
5830
  ctx.strokeStyle=\"rgba(\"+major_color+\",\"+major_opacity+\")\";\
5826
  ctx.lineWidth = line_width;\n\
5831
  ctx.lineWidth = line_width;\n\
5827
  ctx.beginPath();\n\
5832
  ctx.beginPath();\n\
5828
  ctx.moveTo(x,ysize-ymarge);\n\
5833
  ctx.moveTo(x,ysize-ymarge);\n\
5829
  ctx.lineTo(x,0);\n\
5834
  ctx.lineTo(x,0);\n\