OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/canvas2d/EventHitRegion.h" | 5 #include "modules/canvas2d/EventHitRegion.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/html/HTMLCanvasElement.h" | 8 #include "core/html/HTMLCanvasElement.h" |
9 #include "core/layout/LayoutObject.h" | 9 #include "core/layout/LayoutBox.h" |
10 #include "core/page/Page.h" | |
10 #include "modules/canvas2d/CanvasRenderingContext2D.h" | 11 #include "modules/canvas2d/CanvasRenderingContext2D.h" |
11 #include "modules/canvas2d/HitRegion.h" | 12 #include "modules/canvas2d/HitRegion.h" |
12 | 13 |
13 namespace blink { | 14 namespace blink { |
14 | 15 |
15 String EventHitRegion::regionIdFromAbsoluteLocation(HTMLCanvasElement& canvas, c onst LayoutPoint& location) | 16 String EventHitRegion::regionIdFromAbsoluteLocation(HTMLCanvasElement& canvas, c onst LayoutPoint& location) |
16 { | 17 { |
17 CanvasRenderingContext* context = canvas.renderingContext(); | 18 CanvasRenderingContext* context = canvas.renderingContext(); |
18 if (!context || !context->is2d()) | 19 if (!context || !context->is2d()) |
19 return String(); | 20 return String(); |
20 | 21 |
21 Document& document = canvas.document(); | 22 Document& document = canvas.document(); |
22 document.updateLayoutTreeForNodeIfNeeded(&canvas); | 23 document.updateLayoutTreeForNodeIfNeeded(&canvas); |
23 | 24 |
24 // Adjust offsetLocation to be relative to the canvas's position. | 25 // Adjust offsetLocation to be relative to the canvas's position. |
25 LayoutObject* layoutObject = canvas.layoutObject(); | 26 LayoutBoxModelObject* lbmo = canvas.layoutBoxModelObject(); |
26 FloatPoint localPos = layoutObject->absoluteToLocal(FloatPoint(location), Us eTransforms); | 27 FloatPoint localPos = lbmo->absoluteToLocal(FloatPoint(location), UseTransfo rms); |
27 | 28 |
28 LocalFrame* frame = document.frame(); | 29 float width = lbmo->offsetWidth(); |
29 float zoomFactor = frame ? frame->pageZoomFactor() : 1; | 30 float height = lbmo->offsetHeight(); |
30 float scaleFactor = 1 / zoomFactor; | 31 if (lbmo->hasBorderOrPadding()) { |
32 float borderAndPaddingLeft = lbmo->borderLeft() + lbmo->paddingLeft(); | |
33 float borderAndPaddingTop = lbmo->borderTop() + lbmo->paddingTop(); | |
34 localPos.move(-borderAndPaddingLeft, -borderAndPaddingTop); | |
35 width -= lbmo->borderAndPaddingWidth(); | |
36 height -= lbmo->borderAndPaddingHeight(); | |
Justin Novosad
2016/01/05 16:48:39
Isn't there a method that already computes these w
zino
2016/01/06 17:22:31
Done.
| |
37 } | |
38 localPos.scale((canvas.width() / width), (canvas.height() / height)); | |
39 | |
40 Page* page = document.page(); | |
41 float scaleFactor = page->deviceScaleFactor(); | |
31 if (scaleFactor != 1.0f) | 42 if (scaleFactor != 1.0f) |
32 localPos.scale(scaleFactor, scaleFactor); | 43 localPos.scale(scaleFactor, scaleFactor); |
33 | 44 |
34 HitRegion* hitRegion = toCanvasRenderingContext2D(context)->hitRegionAtPoint (localPos); | 45 HitRegion* hitRegion = toCanvasRenderingContext2D(context)->hitRegionAtPoint (localPos); |
35 if (!hitRegion || hitRegion->id().isEmpty()) | 46 if (!hitRegion || hitRegion->id().isEmpty()) |
36 return String(); | 47 return String(); |
37 | 48 |
38 return hitRegion->id(); | 49 return hitRegion->id(); |
39 } | 50 } |
40 | 51 |
41 } // namespace blink | 52 } // namespace blink |
OLD | NEW |