square.lpr 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. library square;
  2. {$mode objfpc}
  3. uses
  4. SysUtils,JS, Web;
  5. Type
  6. TDrawSquare = record
  7. length,x,y : NativeInt;
  8. color : string;
  9. end;
  10. function draw(aCTX : TJSCanvasRenderingContext2D; aLength,aX,aY : NativeInt; aColor : String) : TDrawSquare;
  11. begin
  12. aCtx.fillStyle:=aColor;
  13. aCtx.fillRect(aX, aY, aLength, aLength);
  14. Result.length:=alength;
  15. Result.x:=aX;
  16. Result.y:=aY;
  17. Result.color:=aColor;
  18. end;
  19. Function randomSquare (aCTX : TJSCanvasRenderingContext2D) : TDrawSquare;
  20. var
  21. x,y,l : Integer;
  22. col : string;
  23. begin
  24. Col:=format('rgb(%d,%d,%d)',[Random(256),Random(256),Random(256)]);
  25. X:=Random(481);
  26. Y:=Random(320);
  27. L:=10+Random(9);
  28. Result:=Draw(aCtx,l,x,y,col);
  29. end;
  30. procedure reportArea (aLength : NativeInt; aListID : string);
  31. Var
  32. aItem,aList : TJSHTMLElement;
  33. begin
  34. aItem:=TJSHTMLElement(document.createElement('li'));
  35. aItem.textContent:=Format('Square area is %dpx squared.',[aLength*aLength]);
  36. aList:=TJSHTMLElement(document.getElementById(aListID));
  37. alist.appendChild(aItem);
  38. end;
  39. procedure reportPerimeter (aLength : NativeInt; aListID : string);
  40. Var
  41. aItem,aList : TJSHTMLElement;
  42. begin
  43. aItem:=TJSHTMLElement(document.createElement('li'));
  44. aItem.textContent:=Format('Square perimeter is %dpx.',[aLength*4]);
  45. aList:=TJSHTMLElement(document.getElementById(aListID));
  46. alist.appendChild(aItem);
  47. end;
  48. exports
  49. draw,
  50. randomSquare,
  51. reportArea,
  52. reportPerimeter;
  53. begin
  54. // Your code here
  55. end.