Facebook
From 312312, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 135
  1. <!DOCTYPE html>
  2. &lt;html lang="en"&gt;
  3. &lt;head&gt;
  4.     &lt;meta charset="UTF-8"&gt;
  5.     &lt;meta name="viewport" c initial-scale=1.0"&gt;
  6.     &lt;title&gt;Document&lt;/title&gt;
  7.     [removed][removed]
  8.     &lt;style&gt;
  9.         .test {
  10.             display: flex;
  11.             flex-direction: column;
  12.             gap: 2;
  13.             width: fit-content;
  14.         }
  15.  
  16.         canvas {
  17.             margin-top: 20px;
  18.         }
  19.     &lt;/style&gt;
  20. &lt;/head&gt;
  21. &lt;body&gt;
  22.  
  23.    <div class="test">
  24.     <canvas id="myCanvas" width="600" height="250"  solid black;">
  25.     </canvas>
  26.     <canvas id="myCanvas1" width="600" height="250"  solid black;">
  27.     </canvas>
  28.    </div>
  29.  
  30.     [removed]
  31.  function writeMessage(context, message){
  32. context.font = "18pt Calibri";
  33. context.fillStyle = "black";
  34. context.fillText(message, 10, 25);
  35.  }
  36.  
  37.  window.onload = function(){
  38. events = new Events("myCanvas");
  39. var canvas = events.getCanvas();
  40. var context = events.getContext();
  41. var rectX = canvas.width / 2 - 50;
  42.  
  43. var rectY = canvas.height / 2 - 25;
  44. var draggingRect = false;
  45. var draggingRectOffsetX = 0;
  46. var draggingRectOffsetY = 0;
  47.  
  48. events.setDrawStage(function(){
  49. // określenie współrzędnych wskaźnika myszy
  50. var mousePos = this.getMousePos();
  51. if (draggingRect) {
  52. rectX = mousePos.x - draggingRectOffsetX;
  53. rectY = mousePos.y - draggingRectOffsetY;
  54. }
  55.  
  56. // wyczyszczenie płótna
  57. this.clear();
  58. writeMessage(context, "Przeciąganie i upuszczanie prostokąta...");    
  59. this.beginRegion();
  60. // rysowanie prostokąta
  61. context.beginPath();
  62. context.rect(rectX, rectY, 100, 50);
  63. context.lineWidth = 4;
  64. context.strokeStyle = "black";
  65. context.fillStyle = "#00D2FF";
  66. context.fill();
  67. context.stroke();
  68. context.closePath();
  69. // dodanie procedur obsługi zdarzeń
  70. this.addRegionEventListener("mousedown", function(){
  71. draggingRect = true;
  72. var mousePos = events.getMousePos();
  73. draggingRectOffsetX = mousePos.x - rectX;
  74. draggingRectOffsetY = mousePos.y - rectY;
  75. });
  76. this.addRegionEventListener("mouseup", function(){
  77. draggingRect = false;
  78. });
  79.  
  80. this.addRegionEventListener("mouseover", function(){
  81. document.body.style.cursor = "pointer";
  82. });
  83. this.addRegionEventListener("mouseout", function(){
  84. document.body.style.cursor = "default";
  85. });
  86. this.closeRegion();
  87. });
  88.  };
  89.  
  90.  
  91.     [removed]
  92.  
  93. [removed]
  94.     /*
  95.    * funkcja wczytuje obrazki, a następnie wywołuje
  96.    * po ich pobraniu funkcję zwrotną, przekazując do niej informacje
  97.     * o obrazkach
  98.     */
  99.     function loadImages(sources, callback){
  100.    var loadedImages = 0;
  101.    var numImages = 0;
  102.    var images = {};
  103.    // liczba plików
  104.    for (var src in sources) {
  105.    numImages++;
  106.    }
  107.    // pobranie obrazków
  108.    for (var src in sources) {
  109.    images[src] = new Image();
  110.    images[src].onload = function(){
  111.    // wywołujemy funkcję zwrotną po pobraniu
  112.    // obrazków
  113.    if (++loadedImages >= numImages) {
  114.    callback(images);
  115.    }
  116.    };
  117.    images[src].src = sources[src];
  118.    }
  119.     }
  120.     function drawMagnifier(config){
  121. var context = config.context;
  122.  var images = config.images;
  123. var mousePos = config.mousePos;
  124. var imageX = config.imageX;
  125. var imageY = config.imageY;
  126. var magWidth = config.magWidth;
  127. var magHeight = config.magHeight;
  128. var smallWidth = config.smallWidth;
  129. var smallHeight = config.smallHeight;
  130. var largeWidth = config.largeWidth;
  131. var largeHeight = config.largeHeight
  132.  
  133. /*
  134. * Przy określeniu zmiennych sourceX oraz sourceY zakładamy, że prostokąt
  135. * wycinany z dużej wersji obrazka istnieje w nim. Aby można było
  136. * pokazywać fragmenty spoza granic dużego obrazka,
  137. * należałoby w mechanizmie powiększającym wprowadzić pewne zmiany.
  138. */
  139. var sourceX = ((mousePos.x - imageX) * largeWidth /
  140. smallWidth) - magWidth / 2;
  141. var sourceY = ((mousePos.y - imageY) * largeHeight /
  142. smallHeight) - magHeight / 2;
  143. var destX = mousePos.x - magWidth / 2;
  144. var destY = mousePos.y - magHeight / 2;
  145. var viewWidth = magWidth;
  146. var viewHeight = magHeight;
  147. var viewX = destX;
  148. var viewY = destY;
  149. var drawMagImage = true;
  150. // sprawdzenie krawędzi i modyfikacja,
  151. // gdy mechanizm przekroczy krawędzie dużej wersji obrazka
  152. if (sourceX < 0) {
  153. if (sourceX > -1 * magWidth) {
  154. var diffX = -1 * sourceX;
  155. viewX += diffX;
  156. viewWidth -= diffX;
  157. sourceX = 0;
  158. }
  159. else {
  160. drawMagImage = false;
  161. }
  162. }
  163. if (sourceX > largeWidth - magWidth) {
  164. if (sourceX < largeWidth) {
  165. viewWidth = largeWidth - sourceX;
  166. }
  167. else {
  168. drawMagImage = false;
  169. }
  170. }
  171. if (sourceY < 0) {
  172. if (sourceY > -1 * magHeight) {
  173. var diffY = -1 * sourceY;
  174. viewY += diffY;
  175. viewHeight -= diffY;
  176. sourceY = 0;
  177. }
  178. else {
  179. drawMagImage = false;
  180.  
  181.  
  182. }
  183. }
  184. if (sourceY > largeHeight - magHeight) {
  185. if (sourceY < largeHeight) {
  186. viewHeight = largeHeight - sourceY;
  187. }
  188. else {
  189. drawMagImage = false;
  190. }
  191. }
  192. // rysujemy białe tło okienka z powiększeniem
  193. context.beginPath();
  194.  context.fill
  195. context.fillRect(destX, destY, magWidth, magHeight);
  196. // rysujemy obrazek
  197. if (drawMagImage) {
  198. context.beginPath();
  199. context.drawImage(images.cobraLargeImg, sourceX, sourceY,
  200. viewWidth, viewHeight, viewX, viewY, viewWidth, viewHeight);
  201. }
  202. // rysujemy krawędź okienka z powiększeniem
  203. context.beginPath();
  204. context.lineWidth = 2;
  205.  context.stroke
  206. context.strokeRect(destX, destY, magWidth, magHeight);
  207.  }
  208.  
  209.  function drawImages(images){
  210. var events = new Events("myCanvas1");
  211. var canvas = events.getCanvas();
  212.  var c
  213. // zależności mechanizmu powiększania
  214. var imageX = canvas.width / 2 - images.cobraSmallImg.width / 2;
  215. var imageY = canvas.height / 2 - images.cobraSmallImg.height / 2;
  216. var magWidth = 200;
  217. var magHeight = 150;
  218. var smallWidth = images.cobraSmallImg.width;
  219. var smallHeight = images.cobraSmallImg.height;
  220. var largeWidth = images.cobraLargeImg.width;
  221. var largeHeight = images.cobraLargeImg.height;
  222.  
  223.  
  224. events.setDrawStage(function(){
  225. var mousePos = events.getMousePos();
  226. this.clear();
  227. context.drawImage(images.cobraSmallImg, imageX, imageY,
  228. smallWidth, smallHeight);
  229. // rysujemy prostokąty obszar odpowiadający obrazkowi
  230. context.beginPath();
  231. context.lineWidth = 2;
  232.  context.stroke
  233. context.strokeRect(imageX, imageY, smallWidth, smallHeight);
  234. context.closePath();
  235. if (mousePos !== null) {
  236. drawMagnifier({
  237. context: context,
  238. images: images,
  239. mousePos: mousePos,
  240. imageX: imageX,
  241. imageY: imageY,
  242. magWidth: magWidth,
  243. magHeight: magHeight,
  244. smallWidth: smallWidth,
  245. smallHeight: smallHeight,
  246. largeWidth: largeWidth,
  247. largeHeight: largeHeight
  248. });
  249. }
  250. });
  251.  
  252. canvas.addEventListener("mouseout", function(){
  253. events.drawStage();
  254. }, false);
  255.  }
  256.  
  257.  var sources = {
  258. cobraSmallImg: "cobra_800x600.png",
  259. cobraLargeImg: "cobra_280x210.bmp"
  260. };
  261.  
  262. loadImages(sources, drawImages);
  263.  
  264. [removed]
  265.    
  266. &lt;/body&gt;
  267. &lt;/html&gt;