Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the border to uploaded image using JavaScript

function readURL(event){
		 var getImagePath = URL.createObjectURL(event.target.files[0]);
		 $('#trapezoid').css('background-image', 'url(' + getImagePath + ')');
		}
#clock{
		   background-image:url('');
		   background-size:cover;
		   background-position: center;
		   height: 250px; width: 250px;
		   border: 1px solid #bbb;
			}
      
      #trapezoid {
	border-bottom: 30px solid red;
	border-left: 50px solid transparent;
	border-right: 50px solid transparent;
	height: 80px;
	width:35px;
   -webkit-transform: rotate(93deg);
  -moz-transform: rotate(93deg);
  -o-transform: rotate6(93deg);
  -ms-transform: rotate(93deg);
  transform: rotate(93deg);
  float="left";
  display="inline-block"
}
	
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>

  <input type='file' id='getval' name="background-image" onchange="readURL(event)" /><br/><br/> 
  <div id="trapezoid">
  </div>
  
</body>
</html>

I have this snippet, What I want is when any user uploads any photo then red colored part should get replaced by the uploaded photo instead of whole area.

Please help me how to implement it.

like image 795
Mohammed Avatar asked Apr 25 '26 13:04

Mohammed


1 Answers

You can use perspective, transform, transform-origin, as demonstrated by @Ana at Matrix 3d transform for obtaining trapezoid?

function readURL(event) {
  var getImagePath = URL.createObjectURL(event.target.files[0]);
  $("#trapezoid").css("background", "url(" + getImagePath + ")")
}
.doors {
  margin: 7em auto;
  width: 16em;
  height: 16em;
  perspective: 10em;
  position: absolute;
  top: -50px;
}

#trapezoid {
  display: inline-block;
  width: 50%;
  height: 100%;
  background: red;
}

.doors #trapezoid {
  transform-origin: 0 50% 0;
  transform: rotateY(93deg);
}
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>

<body>

  <input type='file' id='getval' name="background-image" onchange="readURL(event)" /><br/><br/>
  <div class="doors">
    <div id="trapezoid">
    </div>
  </div>

</body>

</html>
like image 50
guest271314 Avatar answered Apr 27 '26 01:04

guest271314